Submission #74742

# Submission time Handle Problem Language Result Execution time Memory
74742 2018-09-07T04:54:24 Z shoemakerjo Pipes (CEOI15_pipes) C++14
100 / 100
1277 ms 6768 KB
#include <bits/stdc++.h>

using namespace std;

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

#define 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 6 ms 2816 KB Output is correct
2 Correct 7 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 2936 KB Output is correct
2 Correct 100 ms 2788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 3120 KB Output is correct
2 Correct 198 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 300 ms 3676 KB Output is correct
2 Correct 260 ms 3872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 404 ms 5616 KB Output is correct
2 Correct 365 ms 5624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 637 ms 5908 KB Output is correct
2 Correct 648 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 810 ms 6652 KB Output is correct
2 Correct 769 ms 6676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1014 ms 6768 KB Output is correct
2 Correct 1079 ms 6652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1277 ms 6448 KB Output is correct
2 Correct 1254 ms 6768 KB Output is correct