Submission #74749

# Submission time Handle Problem Language Result Execution time Memory
74749 2018-09-07T05:01:45 Z shoemakerjo Pipes (CEOI15_pipes) C++14
100 / 100
1407 ms 6740 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int, int>

const int maxn = 100010;
int N, M;
const int mod = 1000000007;
int nums[maxn];
vector<int> adj[maxn];
int par[maxn];

int findset(int a) {
	if (par[a] == a) return a;
	return par[a] = findset(par[a]);
}

void unionset(int a, int b) {
	a = findset(a);
	b = findset(b);
	if (a == b) return;
	if (rand() % 2) {
		par[a] = b;
	}
	else par[b] = a;
}

bool vis[maxn];
// int val;

void dfs(int u, int p) {
	vis[u] = true;
	for (int val : adj[u]) {
		if (val != p) {
			dfs(val, u);
			nums[u] = (nums[val]+nums[u])%mod;
		}

	}
	if (nums[u] == 0 && p != -1) {
		cout << u << " " << p << endl;
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> M;
	for (int i = 1; i <= N; i++) par[i] = i;
	srand(23); //want deterministic for now

	int x, y;
	int cur;
	for (int i = 0; i < M; i++) {
		cin >> x >> y;
		if (findset(x) != findset(y)) {
			adj[x].push_back(y);
			adj[y].push_back(x);
			unionset(x, y);
		}
		else {
			cur = rand()%mod;
			nums[x] += cur + 0LL;
			nums[y] += mod-cur + 0LL;
			nums[x]%=mod;
			nums[y]%=mod;
		}
	}

	for (int i = 1; i <= N; i++) {
		if (!vis[i]) dfs(i, -1);
	}
	
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2816 KB Output is correct
2 Correct 7 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 2884 KB Output is correct
2 Correct 103 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 3120 KB Output is correct
2 Correct 198 ms 3116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 286 ms 3676 KB Output is correct
2 Correct 263 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 425 ms 5488 KB Output is correct
2 Correct 388 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 708 ms 5908 KB Output is correct
2 Correct 668 ms 5920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 828 ms 6644 KB Output is correct
2 Correct 840 ms 6736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1012 ms 6648 KB Output is correct
2 Correct 959 ms 6632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1407 ms 6444 KB Output is correct
2 Correct 1290 ms 6740 KB Output is correct