QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#54611#4194. Killjoys' ConferenceT3alaadl3k2olyehymn3k#TL 19ms42796kbC++2.7kb2022-10-09 20:43:272022-10-09 20:43:27

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-09 20:43:27]
  • 评测
  • 测评结果:TL
  • 用时:19ms
  • 内存:42796kb
  • [2022-10-09 20:43:27]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define gtr(T) vector<T>,greater<T>
#define int long long
using namespace std;
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void err(istream_iterator<string> it) { cerr << endl; }

template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;


const int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};


const int N = 1e6 + 5;
const int M = 1e3 + 5;
const int MOD = 100;

int n, m;

vector<int> adj[N];
int vis[N], parent[N];
bool isCycle = false;
int p;

void addEdge(int u, int v) {
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
}

bool dfs(int v, int par) { // passing vertex and its parent vertex
    vis[v] = true;
    for (int u: adj[v]) {
        if (u == par) continue; // skipping edge to parent vertex
        if (vis[u]) {
            isCycle = 1;
            return true;
        }
        parent[u] = v;
        if (dfs(u, parent[u])) {
            return true;
        }
    }
    return false;
}

int add(int a, int b, int p) {
    return (a + b) % p;
}

int mul(int a, int b, int p) {
    return (1LL * a * b) % p;
}

ll fp(ll base, ll power, ll M) {
    if (power == 1)
        return base;
    ll val = fp(base, power / 2, M);
    return (power % 2 == 0) ? (val * val) % M : (((val * val) % M) * base) % M;
}

void dfs(int u) {
    if (vis[u])return;
    vis[u] = 1;
    for (auto i: adj[u])
        dfs(i);
}

signed main() {
    memset(parent, -1, sizeof parent);
    memset(vis, 0, sizeof vis);
    cin >> n >> m >> p;
    int u, v;
    for (int i = 0; i < m; ++i) {
        cin >> u >> v;
        addEdge(u, v);
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
        if (!vis[i]) {
            cnt++;
            dfs(i);
        };
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; ++i) {
        if (!vis[i] and dfs(i, parent[i])) {
            break;
        }
    }
    if (isCycle) {
        cout << "impossible\n";
    } else {
        int ans = fp(2, cnt - 1, p);
        ans = add(ans, 1, p);
        cout << ans << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 42796kb

input:

4 2 11
1 2
3 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 8ms
memory: 42512kb

input:

5 2 3
1 2
3 4

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 2ms
memory: 42612kb

input:

3 3 11
1 2
2 3
3 1

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 2ms
memory: 42648kb

input:

100 0 13

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 19ms
memory: 42656kb

input:

1000000 0 999983

output:

131073

result:

ok single line: '131073'

Test #6:

score: 0
Accepted
time: 15ms
memory: 42724kb

input:

5 0 17

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 12ms
memory: 42632kb

input:

6 0 11

output:

0

result:

ok single line: '0'

Test #8:

score: -100
Time Limit Exceeded

input:

910292 929828 296851369
138316 521196
344174 594024
83412 434709
773248 836155
238718 422888
306559 904462
267860 898937
413132 488904
377100 725377
280966 647857
194879 226916
46886 181828
211614 264486
597301 659354
211498 340231
131349 463058
218728 850468
112991 832910
311079 403177
94372 457800...

output:


result: