Submission #378323

# Submission time Handle Problem Language Result Execution time Memory
378323 2021-03-16T13:38:18 Z iris2617 Capital City (JOI20_capital_city) C++14
100 / 100
1068 ms 47248 KB
#include<bits/stdc++.h>
#define int long long
#define matsuri pair<int,int>
#define iris 1000000007
using namespace std;

vector<int> G[200010],aoi[200010];
int arr[200010];
int sz[200010],sum,x,pr[200010];
int v[200010],vv[200010],color[200010],tt;
queue<int> q;
bitset<200010> del;

void dfs(int a)
{
	v[a]=tt;
	sz[a]=1;
	for(int b:G[a])
	{
		if(v[b]==tt || del[b])
			continue;
		dfs(b);
		sz[a]+=sz[b];
	}
}

void dc(int a,int f)
{
	int ouo=0;
	for(int b:G[a])
	{
		if(b==f || del[b])
			continue;
		dc(b,a);
		ouo=max(ouo, sz[b]);
	}
	ouo=max(ouo, sum-sz[a]);
	if(ouo<=sum/2)
		x=a;
}

void dfs2(int a,int f)
{
	pr[a]=f;
	for(int b:G[a])
	{
		if(b==f || del[b])
			continue;
		dfs2(b,a);
	}
}

int sana(int a)
{
	tt++;
	dfs(a);
	sum=sz[a];
	dc(a,0);
	dfs2(x,0);
	
	int ans=0;
	bool flag=0;
	del[x]=1;
	vv[x]=tt;
	color[arr[x]]=tt;
	q.push(arr[x]);
	while(!q.empty())
	{
		int k=q.front();
		q.pop();
		for(int a:aoi[k])
		{
			if(v[a]!=tt)
			{
				flag=1;
				break;
			}
			while(vv[a]!=tt)
			{
				vv[a]=tt;
				if(color[arr[a]]!=tt)
				{
					color[arr[a]]=tt;
					q.push(arr[a]);
					ans++;
				}
				a=pr[a];
			}
		}
	}
	if(flag)
		ans=1e9;
//	cout<<' '<<x<<' '<<ans<<'\n';
	
	for(int b:G[x])
	{
		if(del[b])
			continue;
		ans=min(ans, sana(b));
	}
	return ans;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n,k,a,b,i;
	
	cin>>n>>k;
	for(i=1;i<n;i++)
	{
		cin>>a>>b;
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	for(i=1;i<=n;i++)
	{
		cin>>a;
		arr[i]=a;
		aoi[a].emplace_back(i);
	}
	cout<<sana(1)<<'\n';
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 6 ms 9856 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 6 ms 9856 KB Output is correct
9 Correct 6 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 6 ms 9856 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 6 ms 9856 KB Output is correct
9 Correct 6 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 8 ms 10092 KB Output is correct
13 Correct 8 ms 9964 KB Output is correct
14 Correct 11 ms 9964 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 9 ms 10092 KB Output is correct
17 Correct 9 ms 10092 KB Output is correct
18 Correct 8 ms 10092 KB Output is correct
19 Correct 8 ms 10092 KB Output is correct
20 Correct 8 ms 10092 KB Output is correct
21 Correct 8 ms 10092 KB Output is correct
22 Correct 8 ms 10112 KB Output is correct
23 Correct 8 ms 10092 KB Output is correct
24 Correct 8 ms 10092 KB Output is correct
25 Correct 10 ms 10092 KB Output is correct
26 Correct 10 ms 10092 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 10 ms 10112 KB Output is correct
29 Correct 9 ms 10112 KB Output is correct
30 Correct 9 ms 10092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 704 ms 42988 KB Output is correct
2 Correct 223 ms 43372 KB Output is correct
3 Correct 742 ms 42604 KB Output is correct
4 Correct 226 ms 43500 KB Output is correct
5 Correct 662 ms 39660 KB Output is correct
6 Correct 222 ms 47248 KB Output is correct
7 Correct 692 ms 43880 KB Output is correct
8 Correct 243 ms 46808 KB Output is correct
9 Correct 799 ms 42348 KB Output is correct
10 Correct 806 ms 39788 KB Output is correct
11 Correct 843 ms 42976 KB Output is correct
12 Correct 844 ms 45292 KB Output is correct
13 Correct 826 ms 39276 KB Output is correct
14 Correct 777 ms 45548 KB Output is correct
15 Correct 806 ms 45292 KB Output is correct
16 Correct 788 ms 40356 KB Output is correct
17 Correct 825 ms 40912 KB Output is correct
18 Correct 754 ms 41068 KB Output is correct
19 Correct 796 ms 44380 KB Output is correct
20 Correct 759 ms 46040 KB Output is correct
21 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 6 ms 9856 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 6 ms 9856 KB Output is correct
9 Correct 6 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 8 ms 10092 KB Output is correct
13 Correct 8 ms 9964 KB Output is correct
14 Correct 11 ms 9964 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 9 ms 10092 KB Output is correct
17 Correct 9 ms 10092 KB Output is correct
18 Correct 8 ms 10092 KB Output is correct
19 Correct 8 ms 10092 KB Output is correct
20 Correct 8 ms 10092 KB Output is correct
21 Correct 8 ms 10092 KB Output is correct
22 Correct 8 ms 10112 KB Output is correct
23 Correct 8 ms 10092 KB Output is correct
24 Correct 8 ms 10092 KB Output is correct
25 Correct 10 ms 10092 KB Output is correct
26 Correct 10 ms 10092 KB Output is correct
27 Correct 9 ms 10092 KB Output is correct
28 Correct 10 ms 10112 KB Output is correct
29 Correct 9 ms 10112 KB Output is correct
30 Correct 9 ms 10092 KB Output is correct
31 Correct 704 ms 42988 KB Output is correct
32 Correct 223 ms 43372 KB Output is correct
33 Correct 742 ms 42604 KB Output is correct
34 Correct 226 ms 43500 KB Output is correct
35 Correct 662 ms 39660 KB Output is correct
36 Correct 222 ms 47248 KB Output is correct
37 Correct 692 ms 43880 KB Output is correct
38 Correct 243 ms 46808 KB Output is correct
39 Correct 799 ms 42348 KB Output is correct
40 Correct 806 ms 39788 KB Output is correct
41 Correct 843 ms 42976 KB Output is correct
42 Correct 844 ms 45292 KB Output is correct
43 Correct 826 ms 39276 KB Output is correct
44 Correct 777 ms 45548 KB Output is correct
45 Correct 806 ms 45292 KB Output is correct
46 Correct 788 ms 40356 KB Output is correct
47 Correct 825 ms 40912 KB Output is correct
48 Correct 754 ms 41068 KB Output is correct
49 Correct 796 ms 44380 KB Output is correct
50 Correct 759 ms 46040 KB Output is correct
51 Correct 7 ms 9836 KB Output is correct
52 Correct 641 ms 31764 KB Output is correct
53 Correct 630 ms 31724 KB Output is correct
54 Correct 648 ms 31596 KB Output is correct
55 Correct 632 ms 31724 KB Output is correct
56 Correct 672 ms 31852 KB Output is correct
57 Correct 611 ms 31724 KB Output is correct
58 Correct 591 ms 35692 KB Output is correct
59 Correct 541 ms 36136 KB Output is correct
60 Correct 699 ms 34668 KB Output is correct
61 Correct 741 ms 34284 KB Output is correct
62 Correct 227 ms 47084 KB Output is correct
63 Correct 240 ms 47212 KB Output is correct
64 Correct 222 ms 46956 KB Output is correct
65 Correct 224 ms 47084 KB Output is correct
66 Correct 456 ms 39392 KB Output is correct
67 Correct 434 ms 39264 KB Output is correct
68 Correct 413 ms 39264 KB Output is correct
69 Correct 425 ms 39264 KB Output is correct
70 Correct 474 ms 39264 KB Output is correct
71 Correct 465 ms 39132 KB Output is correct
72 Correct 413 ms 39136 KB Output is correct
73 Correct 506 ms 38368 KB Output is correct
74 Correct 419 ms 39264 KB Output is correct
75 Correct 430 ms 39264 KB Output is correct
76 Correct 658 ms 38636 KB Output is correct
77 Correct 672 ms 36844 KB Output is correct
78 Correct 908 ms 40732 KB Output is correct
79 Correct 846 ms 38764 KB Output is correct
80 Correct 922 ms 45784 KB Output is correct
81 Correct 1030 ms 42372 KB Output is correct
82 Correct 890 ms 42368 KB Output is correct
83 Correct 904 ms 39080 KB Output is correct
84 Correct 897 ms 44992 KB Output is correct
85 Correct 987 ms 43372 KB Output is correct
86 Correct 934 ms 38652 KB Output is correct
87 Correct 883 ms 40556 KB Output is correct
88 Correct 725 ms 42092 KB Output is correct
89 Correct 815 ms 38216 KB Output is correct
90 Correct 787 ms 37724 KB Output is correct
91 Correct 708 ms 40160 KB Output is correct
92 Correct 734 ms 39020 KB Output is correct
93 Correct 732 ms 38764 KB Output is correct
94 Correct 755 ms 37740 KB Output is correct
95 Correct 783 ms 39276 KB Output is correct
96 Correct 742 ms 38244 KB Output is correct
97 Correct 1068 ms 40172 KB Output is correct