Submission #136326

# Submission time Handle Problem Language Result Execution time Memory
136326 2019-07-25T07:03:40 Z 김세빈(#3258) Link (CEOI06_link) C++14
72 / 100
473 ms 48648 KB
#include <bits/stdc++.h>

using namespace std;

vector <int> V[505050];
int D[505050], P[505050];
int K[505050];
int X[20][505050];
int chk1[505050], chk2[505050];
int n, k, ans;

void dfs(int p, int v)
{
	for(int &t: V[p]){
		dfs(t, 0);
		K[p] = max(K[p], K[t] - 1);
	}
	
	if(!v && !K[p]){
		ans ++; K[p] = k;
	}
}

int calc(vector <int> &V)
{
	int m, i, j, t, s, ret;
	
	ret = 1e9;
	
	m = V.size();
	for(i=0; i<m; i++){
		V.push_back(V[i]);
	}
	
	for(i=0, j=0; i<m+m; i++){
		for(; j<m+m && (j - i <= k || !V[j]); j++);
		X[0][i] = j;
	}
	
	X[0][m + m] = m + m;
	
	for(i=1; i<=19; i++){
		for(j=0; j<=m+m; j++){
			X[i][j] = X[i - 1][X[i - 1][j]];
		}
	}
	
	for(i=0; i<m; i++){
		s = 1; t = i;
		for(j=19; j>=0; j--){
			if(X[j][t] < m + i){
				t = X[j][t];
				s += 1 << j;
			}
		}
		ret = min(ret, s);
	}
	
	return ret;
}

int main()
{
	vector <int> T;
	int i, j, x, y;
	
	scanf("%d%d", &n, &k);
	
	for(i=1; i<=n; i++){
		scanf("%d%d", &x, &y);
		P[x] = y; D[y] ++;
	}
	
	K[1] = k + 1;
	
	for(i=1; i<=n; i++){
		for(j=i; !chk1[j]; j=P[j]) chk1[j] = i;
		if(chk1[j] != i) continue;
		for(; !chk2[j]; j=P[j]) chk2[j] = 1;
	}
	
	for(i=1; i<=n; i++){
		if(!chk2[i]){
			V[P[i]].push_back(i);
		}
	}
	
	for(i=1; i<=n; i++){
		if(chk2[i]) dfs(i, 1);
	}
	
	for(i=1; i<=n; i++){
		if(chk2[i]){
			for(j=i; chk2[j]; j=P[j]){
				K[P[j]] = max(K[P[j]], K[j] - 1);
				chk2[j] = 0;
			}
			
			for(j=i; chk1[j]; j=P[j]){
				K[P[j]] = max(K[P[j]], K[j] - 1);
				chk1[j] = 0;
			}
			
			T.clear();
			
			x = 0;
			
			for(j=i; !chk1[j]; j=P[j]){
				T.push_back(K[j]? 0 : 1);
				if(!K[j]) x ++;
				chk1[j] = 1;
			}
			
			if(x) ans += calc(T);
		}
	}
	
	printf("%d\n", ans);
	
	return 0;
}

Compilation message

link.cpp: In function 'int main()':
link.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~
link.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 12408 KB Output isn't correct
2 Incorrect 12 ms 12408 KB Output isn't correct
3 Correct 13 ms 12408 KB Output is correct
4 Incorrect 14 ms 12536 KB Output isn't correct
5 Incorrect 18 ms 12664 KB Output isn't correct
6 Correct 28 ms 14456 KB Output is correct
7 Correct 38 ms 15220 KB Output is correct
8 Correct 50 ms 17056 KB Output is correct
9 Correct 75 ms 22776 KB Output is correct
10 Correct 70 ms 20724 KB Output is correct
11 Correct 114 ms 38268 KB Output is correct
12 Incorrect 148 ms 21412 KB Output isn't correct
13 Correct 197 ms 32496 KB Output is correct
14 Correct 244 ms 34028 KB Output is correct
15 Correct 311 ms 38384 KB Output is correct
16 Incorrect 382 ms 46084 KB Output isn't correct
17 Correct 379 ms 41428 KB Output is correct
18 Incorrect 458 ms 41712 KB Output isn't correct
19 Correct 463 ms 41872 KB Output is correct
20 Correct 423 ms 41196 KB Output is correct
21 Correct 428 ms 46000 KB Output is correct
22 Correct 438 ms 45868 KB Output is correct
23 Correct 473 ms 48648 KB Output is correct
24 Correct 454 ms 45020 KB Output is correct
25 Correct 442 ms 43756 KB Output is correct