답안 #250659

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
250659 2020-07-18T15:26:53 Z groeneprof Unique Cities (JOI19_ho_t5) C++14
100 / 100
989 ms 71272 KB
#include <bits/stdc++.h>
#define int long long
#define P(x) do {if(debug) cout << x << endl;} while(false)
#define H(x) P(#x << ": " << x)
#define FR(i, a, b) for(int i = (a); i < (b); ++i)
#define F(i, n) FR(i, 0, n)
#define DR(i, a, b) for(int i = (b); i --> (a);)
#define D(i, n) DR(i, 0, n)
#define S(s) (int)(s).size()
#define ALL(x) (x).begin(), (x).end()
#define MI(x, a) (x) = min(x, a)
#define MA(x, a) (x) = max(x, a)
#define V vector
#define pb push_back
#define mp make_pair
using namespace std;
const bool debug = 0;
const int inf = 1e18;
int n,m;
vector<vector<int> > graph;
vector<int> color;
vector<int> par;
vector<bool> isdiam;
vector<int> maxdepth;
vector<int> subdiam;
vector<int> diam;
vector<bool> prohibited;
vector<int> half;
vector<int> halfbol;
vector<int> res;

int deepest=0; int deepestdepth=0;
int root;
int dfs1(int u, int p, int depth){
	par[u] = p;
	if(depth > deepestdepth){
		deepestdepth = depth;
		deepest = u;
	}
	maxdepth[u] = depth;
	subdiam[u] = 1;
	half[u] = u;
	halfbol[u] = false;

	int b = 0; int c = 0;
	for(int i = 0; i<graph[u].size(); i++) if(graph[u][i]!=p){
		int v = graph[u][i];
		int a = dfs1(v,u,depth+1);
		maxdepth[u] = max(maxdepth[u],a);
		subdiam[u] = max(subdiam[u], subdiam[v]);
		if(a>=b){
			c = b;
			b = a;
			swap(graph[u][i], graph[u][0]);
			half[u] = half[v];
			if(halfbol[v]){
				half[u] = par[half[u]];
			}
			halfbol[u] = !halfbol[v];

		}
		else if(a>c){
			c = a;
		}
	}
	subdiam[u] = max(subdiam[u], b+c-2*depth+1);
	return maxdepth[u];
}

vector<int> uniquec;
int cnt = 0;
vector<vector<int> > flagsubtree;
vector<vector<int> > flagall;
/*
	process flagsubtree
	set flag on subtrees if conditions are met
	set flag on ancestor if conditions is met
	dfs (process flagall in the meantime)
	undo flagsubtree
*/
void dfs2(int u, int d){
	H(u);
	for(int c : flagsubtree[u]){
		if(uniquec[c] == 0) cnt++;  
		uniquec[c]++;
	}

	for(int v : graph[u]) if(v!=par[u]){
		H(v);
		H(maxdepth[v]);
		H(subdiam[v]);
		if(maxdepth[v]-d+1 > subdiam[v]) {
			flagsubtree[half[v]].push_back(color[u]);
			P("set flag for color "<<color[u]<<" on "<<half[v]);
		}
	}

	if(isdiam[u] && !prohibited[u] && u != root){
		flagall[diam[(d-1)/2]].push_back(color[u]);
		P("flagall for color "<<color[u]<<" on "<<diam[(d-1)/2]);
	}

	for(int v : graph[u]) if(v!=par[u]){
		dfs2(v,d+1);
		if(!flagall[u].empty()){
			for(int c : flagall[u]){
				if(uniquec[c]==0) cnt++;
				uniquec[c]++;
			}
			flagall[u].clear();
		}
	}
	res[u] = cnt;
	for(int c : flagsubtree[u]){
		if(uniquec[c] == 1) cnt--;  
		uniquec[c]--;
	}	
	P("exit "<<u);

}



signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m;
	graph.resize(n);
	color.resize(n);
	F(i,n-1){
		int a,b;
		cin>>a>>b;
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	F(i,n){
		cin>>color[i];
		color[i]--;
	}
	par.resize(n);
	maxdepth.resize(n);
	isdiam.resize(n,false);
	subdiam.resize(n);
	halfbol.resize(n);
	half.resize(n);
	dfs1(0,0,0);
	root = deepest;
	H(root);
	deepestdepth = 0;
	dfs1(root,-1,0);

	P('a');
	for(int u = deepest; u!=-1; u = par[u]){
		isdiam[u] = true;
		diam.push_back(u);
	}
	reverse(diam.begin(), diam.end());
	
	//for(int i : diam){
	//	cout<<i<<" ";
	//}
	//cout<<endl;
	prohibited.resize(n, false);
	int j = -1;
	for(int i = 0; i<diam.size(); i++){		
		int u = diam[i];
		if(j>=i){
			prohibited[diam[i]] = true;
		}
		for(int v : graph[u]) if(v!=par[u] && !isdiam[v]){
			j = max(j, maxdepth[v]);
		}
	}
	//for(bool b : prohibited){
	//	cout<<b<<" ";
	//}
	//cout<<endl;
	P('b');
	res.resize(n);
	flagall.resize(n);
	flagsubtree.resize(n);
	uniquec.resize(n,0);
	dfs2(root,0);


	for(int i : res){
		cout<<i<<endl;
	}
	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'long long int dfs1(long long int, long long int, long long int)':
joi2019_ho_t5.cpp:46:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i<graph[u].size(); i++) if(graph[u][i]!=p){
                 ~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:166:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i<diam.size(); i++){  
                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 5 ms 768 KB Output is correct
6 Correct 9 ms 1024 KB Output is correct
7 Correct 5 ms 896 KB Output is correct
8 Correct 6 ms 768 KB Output is correct
9 Correct 6 ms 768 KB Output is correct
10 Correct 6 ms 768 KB Output is correct
11 Correct 6 ms 768 KB Output is correct
12 Correct 5 ms 768 KB Output is correct
13 Correct 7 ms 1024 KB Output is correct
14 Correct 5 ms 896 KB Output is correct
15 Correct 9 ms 896 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 6 ms 896 KB Output is correct
18 Correct 6 ms 896 KB Output is correct
19 Correct 6 ms 768 KB Output is correct
20 Correct 6 ms 1024 KB Output is correct
21 Correct 6 ms 896 KB Output is correct
22 Correct 5 ms 768 KB Output is correct
23 Correct 6 ms 856 KB Output is correct
24 Correct 5 ms 768 KB Output is correct
25 Correct 6 ms 768 KB Output is correct
26 Correct 6 ms 768 KB Output is correct
27 Correct 6 ms 1012 KB Output is correct
28 Correct 6 ms 896 KB Output is correct
29 Correct 5 ms 896 KB Output is correct
30 Correct 6 ms 768 KB Output is correct
31 Correct 6 ms 896 KB Output is correct
32 Correct 6 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 432 ms 22264 KB Output is correct
2 Correct 463 ms 40024 KB Output is correct
3 Correct 80 ms 7928 KB Output is correct
4 Correct 847 ms 40952 KB Output is correct
5 Correct 891 ms 69608 KB Output is correct
6 Correct 763 ms 53356 KB Output is correct
7 Correct 718 ms 41460 KB Output is correct
8 Correct 777 ms 43892 KB Output is correct
9 Correct 708 ms 42744 KB Output is correct
10 Correct 821 ms 42744 KB Output is correct
11 Correct 712 ms 44160 KB Output is correct
12 Correct 989 ms 58848 KB Output is correct
13 Correct 862 ms 55660 KB Output is correct
14 Correct 829 ms 53484 KB Output is correct
15 Correct 670 ms 44392 KB Output is correct
16 Correct 770 ms 61276 KB Output is correct
17 Correct 741 ms 54380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 538 ms 30072 KB Output is correct
2 Correct 858 ms 68200 KB Output is correct
3 Correct 93 ms 9080 KB Output is correct
4 Correct 715 ms 41720 KB Output is correct
5 Correct 876 ms 71272 KB Output is correct
6 Correct 794 ms 54508 KB Output is correct
7 Correct 859 ms 41964 KB Output is correct
8 Correct 724 ms 47344 KB Output is correct
9 Correct 778 ms 45564 KB Output is correct
10 Correct 786 ms 43896 KB Output is correct
11 Correct 829 ms 42988 KB Output is correct
12 Correct 910 ms 65744 KB Output is correct
13 Correct 790 ms 54764 KB Output is correct
14 Correct 916 ms 53868 KB Output is correct
15 Correct 672 ms 45416 KB Output is correct
16 Correct 789 ms 62564 KB Output is correct
17 Correct 782 ms 55432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 5 ms 768 KB Output is correct
6 Correct 9 ms 1024 KB Output is correct
7 Correct 5 ms 896 KB Output is correct
8 Correct 6 ms 768 KB Output is correct
9 Correct 6 ms 768 KB Output is correct
10 Correct 6 ms 768 KB Output is correct
11 Correct 6 ms 768 KB Output is correct
12 Correct 5 ms 768 KB Output is correct
13 Correct 7 ms 1024 KB Output is correct
14 Correct 5 ms 896 KB Output is correct
15 Correct 9 ms 896 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 6 ms 896 KB Output is correct
18 Correct 6 ms 896 KB Output is correct
19 Correct 6 ms 768 KB Output is correct
20 Correct 6 ms 1024 KB Output is correct
21 Correct 6 ms 896 KB Output is correct
22 Correct 5 ms 768 KB Output is correct
23 Correct 6 ms 856 KB Output is correct
24 Correct 5 ms 768 KB Output is correct
25 Correct 6 ms 768 KB Output is correct
26 Correct 6 ms 768 KB Output is correct
27 Correct 6 ms 1012 KB Output is correct
28 Correct 6 ms 896 KB Output is correct
29 Correct 5 ms 896 KB Output is correct
30 Correct 6 ms 768 KB Output is correct
31 Correct 6 ms 896 KB Output is correct
32 Correct 6 ms 896 KB Output is correct
33 Correct 432 ms 22264 KB Output is correct
34 Correct 463 ms 40024 KB Output is correct
35 Correct 80 ms 7928 KB Output is correct
36 Correct 847 ms 40952 KB Output is correct
37 Correct 891 ms 69608 KB Output is correct
38 Correct 763 ms 53356 KB Output is correct
39 Correct 718 ms 41460 KB Output is correct
40 Correct 777 ms 43892 KB Output is correct
41 Correct 708 ms 42744 KB Output is correct
42 Correct 821 ms 42744 KB Output is correct
43 Correct 712 ms 44160 KB Output is correct
44 Correct 989 ms 58848 KB Output is correct
45 Correct 862 ms 55660 KB Output is correct
46 Correct 829 ms 53484 KB Output is correct
47 Correct 670 ms 44392 KB Output is correct
48 Correct 770 ms 61276 KB Output is correct
49 Correct 741 ms 54380 KB Output is correct
50 Correct 538 ms 30072 KB Output is correct
51 Correct 858 ms 68200 KB Output is correct
52 Correct 93 ms 9080 KB Output is correct
53 Correct 715 ms 41720 KB Output is correct
54 Correct 876 ms 71272 KB Output is correct
55 Correct 794 ms 54508 KB Output is correct
56 Correct 859 ms 41964 KB Output is correct
57 Correct 724 ms 47344 KB Output is correct
58 Correct 778 ms 45564 KB Output is correct
59 Correct 786 ms 43896 KB Output is correct
60 Correct 829 ms 42988 KB Output is correct
61 Correct 910 ms 65744 KB Output is correct
62 Correct 790 ms 54764 KB Output is correct
63 Correct 916 ms 53868 KB Output is correct
64 Correct 672 ms 45416 KB Output is correct
65 Correct 789 ms 62564 KB Output is correct
66 Correct 782 ms 55432 KB Output is correct
67 Correct 73 ms 6136 KB Output is correct
68 Correct 329 ms 30952 KB Output is correct
69 Correct 461 ms 33844 KB Output is correct
70 Correct 704 ms 40976 KB Output is correct
71 Correct 878 ms 69736 KB Output is correct
72 Correct 824 ms 53428 KB Output is correct
73 Correct 675 ms 41092 KB Output is correct
74 Correct 721 ms 45936 KB Output is correct
75 Correct 712 ms 43380 KB Output is correct
76 Correct 725 ms 43128 KB Output is correct
77 Correct 730 ms 42224 KB Output is correct
78 Correct 872 ms 62316 KB Output is correct
79 Correct 767 ms 59208 KB Output is correct
80 Correct 790 ms 51956 KB Output is correct
81 Correct 610 ms 44440 KB Output is correct
82 Correct 809 ms 61164 KB Output is correct
83 Correct 764 ms 54124 KB Output is correct
84 Correct 721 ms 41336 KB Output is correct
85 Correct 945 ms 70504 KB Output is correct
86 Correct 794 ms 53876 KB Output is correct
87 Correct 729 ms 41468 KB Output is correct
88 Correct 752 ms 46960 KB Output is correct
89 Correct 729 ms 44148 KB Output is correct
90 Correct 771 ms 43512 KB Output is correct
91 Correct 770 ms 42772 KB Output is correct
92 Correct 865 ms 69604 KB Output is correct
93 Correct 718 ms 51564 KB Output is correct
94 Correct 722 ms 48496 KB Output is correct
95 Correct 573 ms 44652 KB Output is correct
96 Correct 756 ms 61672 KB Output is correct
97 Correct 795 ms 54764 KB Output is correct
98 Correct 772 ms 41784 KB Output is correct
99 Correct 907 ms 70804 KB Output is correct
100 Correct 763 ms 54380 KB Output is correct
101 Correct 850 ms 41928 KB Output is correct
102 Correct 779 ms 46092 KB Output is correct
103 Correct 705 ms 44288 KB Output is correct
104 Correct 717 ms 43640 KB Output is correct
105 Correct 638 ms 43116 KB Output is correct
106 Correct 776 ms 57964 KB Output is correct
107 Correct 765 ms 59756 KB Output is correct
108 Correct 729 ms 50160 KB Output is correct
109 Correct 601 ms 45356 KB Output is correct
110 Correct 818 ms 62436 KB Output is correct
111 Correct 728 ms 55536 KB Output is correct