Submission #54899

# Submission time Handle Problem Language Result Execution time Memory
54899 2018-07-05T09:16:01 Z 김세빈(#1509) Pipes (CEOI15_pipes) C++11
100 / 100
1562 ms 8004 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

vector <int> V[101010];
int P[101010], K[101010];
int R[101010], D[101010], S[101010];
bool chk[101010];
int n, m;

void reorder(int x, int p, int r, bool f)
{
	int i, c;
	bool f2;
	
	D[p] = D[r] + 1; R[p] = x;
	c = K[p], K[p] = r;
	
	if(c != r) f2 = chk[p], chk[p] = f;
	
	if(chk[p]) P[p] = P[r];
	else P[p] = p;
	
	for(auto &t: V[p]){
		if(t != r){
			if(t == c) reorder(x, t, p, f2);
			else reorder(x, t, p, 0);
		}
	}
	
	if(P[p] == p) P[p] = r;
}

int color(int a, int b)
{
	if(a == b) return a;
	if(D[a] > D[b]) chk[a] = 1, P[a] = color(P[a], b);
	else chk[b] = 1, P[b] = color(a, P[b]);
}

int main()
{
	int i, a, b;
	
	scanf("%d%d", &n, &m);
	
	for(i=1;i<=n;i++){
		S[i] = 1;
		R[i] = i;
	}
	
	for(i=1;i<=m;i++){
		scanf("%d%d", &a, &b);
		if(R[a] != R[b]){
			if(S[R[a]] > S[R[b]]) swap(a, b);
			
			V[a].push_back(b);
			V[b].push_back(a);
			
			S[R[b]] += S[R[a]];
			reorder(R[b], a, b, 0);
		}
		else color(a, b);
	}
	
	for(i=1;i<=n;i++){
		if(R[i] != i && !chk[i]) printf("%d %d\n", i, K[i]);
	}
	
	return 0;
}

Compilation message

pipes.cpp: In function 'void reorder(int, int, int, bool)':
pipes.cpp:15:6: warning: unused variable 'i' [-Wunused-variable]
  int i, c;
      ^
pipes.cpp: In function 'int color(int, int)':
pipes.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
pipes.cpp: In function 'int main()':
pipes.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp: In function 'void reorder(int, int, int, bool)':
pipes.cpp:28:22: warning: 'f2' may be used uninitialized in this function [-Wmaybe-uninitialized]
    if(t == c) reorder(x, t, p, f2);
               ~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 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 2944 KB Output is correct
2 Correct 8 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 2816 KB Output is correct
2 Correct 121 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 214 ms 3344 KB Output is correct
2 Correct 263 ms 3276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 363 ms 4040 KB Output is correct
2 Correct 323 ms 4176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 499 ms 6328 KB Output is correct
2 Correct 489 ms 6388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 786 ms 6856 KB Output is correct
2 Correct 758 ms 6884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1033 ms 7876 KB Output is correct
2 Correct 996 ms 7980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1299 ms 7888 KB Output is correct
2 Correct 1242 ms 7936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1562 ms 7744 KB Output is correct
2 Correct 1499 ms 8004 KB Output is correct