Submission #113083

# Submission time Handle Problem Language Result Execution time Memory
113083 2019-05-23T14:29:30 Z gs14004 Unique Cities (JOI19_ho_t5) C++17
100 / 100
739 ms 42308 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
using pi = pair<int, int>;

vector<int> gph[MAXN];
int n, m, r[2], dist[2][MAXN];
int a[MAXN], par[MAXN];
pi dp[MAXN];

void dfs_far(int x, int p){
	par[x] = p;
	dp[x] = pi(0, x);
	for(auto &i : gph[x]){
		if(i != p){
			dfs_far(i, x);
			dp[x] = max(dp[x], pi(dp[i].first + 1, i));
		}
	}
}

struct jaryoguzo{
	vector<int> v;
	int mark[MAXN];
	int cnt[MAXN], nonZeroes;
	void upd(int x, int v){
		if(cnt[x] > 0) nonZeroes--;
		cnt[x] += v;
		if(cnt[x] > 0) nonZeroes++;
	}
	void push_back(int x){
		v.push_back(x);
		upd(x, +1);
	}
	void pop_back(){
		upd(v.back(), -1);
		v.pop_back();
	}
	void mark_interval(int s, int e, int d){
		for(int i=s; i<e && i<v.size(); i++){
			if(mark[v.size() - 1 - i] == 0) upd(v[v.size() - 1 - i], -1);
			mark[v.size()-1-i] += d;
			if(mark[v.size() - 1 - i] == 0) upd(v[v.size() - 1 - i], +1);
		}
	}
	int query(){ return nonZeroes; }
}JG;

int ret[MAXN];

void dfs_calc(int x, int p){
	vector<int> pth, sndmax;
	for(int j=x; ; j=dp[j].second){
		int smax = 0;
		for(auto &k : gph[j]){
			if(k != par[j] && k != dp[j].second){
				smax = max(smax, dp[k].first + 1); 
			}
		}
		pth.push_back(j);
		sndmax.push_back(smax);
		if(j == dp[j].second) break;
	}
	int pointer = (int)pth.size() - 1;
	JG.mark_interval(0, pointer, +1);
	for(int i = 0; i < pth.size(); i++){
		ret[pth[i]] = JG.query();
		JG.push_back(a[pth[i]]);
		for(auto &j : gph[pth[i]]){
			if(j != par[pth[i]] && dp[pth[i]].second != j){
				dfs_calc(j, pth[i]);
			}
		}
		pointer += 1;
		if(i + 1 < pth.size()) JG.mark_interval(0, 1, +1);
		if(i + 1 < pth.size()) JG.mark_interval(pointer - 2 * i - 2, pointer - 2 * i, -1);
		if(i + 1 < pth.size()) JG.mark_interval(1, 1 + sndmax[i], +1);
	}
	for(int i = (int)pth.size() - 1; i >= 0; i--){
		if(i + 1 < pth.size()) JG.mark_interval(1, 1 + sndmax[i], -1);
		if(i + 1 < pth.size()) JG.mark_interval(pointer - 2 * i - 2, pointer - 2 * i, +1);
		if(i + 1 < pth.size()) JG.mark_interval(0, 1, -1);
		pointer -= 1;
		JG.pop_back();
	}
	JG.mark_interval(0, pointer, -1);
}

vector<int> solve(int r){
	dfs_far(r, -1);
	dfs_calc(r, -1);
	return vector<int>(ret + 1, ret + n + 1);
}

void fill_dist(int x, int p, int d, int l){
	dist[l][x] = d;
	for(auto &i : gph[x]){
		if(i != p){
			fill_dist(i, x, d + 1, l);
		}
	}
}

pi dfs(int x, int p){
	pi ret(0, x);
	for(auto &i : gph[x]){
		if(i != p){
			auto y = dfs(i, x);
			y.first++;
			ret = max(ret, y);
		}
	}
	return ret;
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0; i<n-1; i++){
		int s, e; scanf("%d %d",&s,&e);
		gph[s].push_back(e);
		gph[e].push_back(s);
	}
	for(int i=1; i<=n; i++) scanf("%d",&a[i]);
	r[0] = dfs(1, 0).second;
	r[1] = dfs(r[0], 0).second;
	fill_dist(r[0], -1, 0, 0);
	fill_dist(r[1], -1, 0, 1);
	auto v1 = solve(r[0]);
	auto v2 = solve(r[1]);
	for(int i=1; i<=n; i++){
		if(dist[0][i] <= dist[1][i]) printf("%d\n", v2[i-1]);
		else printf("%d\n", v1[i-1]);
	}
}

Compilation message

joi2019_ho_t5.cpp: In member function 'void jaryoguzo::mark_interval(int, int, int)':
joi2019_ho_t5.cpp:40:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=s; i<e && i<v.size(); i++){
                       ~^~~~~~~~~
joi2019_ho_t5.cpp: In function 'void dfs_calc(int, int)':
joi2019_ho_t5.cpp:66:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < pth.size(); i++){
                 ~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:75:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i + 1 < pth.size()) JG.mark_interval(0, 1, +1);
      ~~~~~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:76:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i + 1 < pth.size()) JG.mark_interval(pointer - 2 * i - 2, pointer - 2 * i, -1);
      ~~~~~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:77:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i + 1 < pth.size()) JG.mark_interval(1, 1 + sndmax[i], +1);
      ~~~~~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:80:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i + 1 < pth.size()) JG.mark_interval(1, 1 + sndmax[i], -1);
      ~~~~~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:81:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i + 1 < pth.size()) JG.mark_interval(pointer - 2 * i - 2, pointer - 2 * i, +1);
      ~~~~~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:82:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i + 1 < pth.size()) JG.mark_interval(0, 1, -1);
      ~~~~~~^~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:119:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, e; scanf("%d %d",&s,&e);
             ~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:123:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1; i<=n; i++) scanf("%d",&a[i]);
                          ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5092 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 6 ms 5220 KB Output is correct
4 Correct 8 ms 5220 KB Output is correct
5 Correct 9 ms 5220 KB Output is correct
6 Correct 7 ms 5376 KB Output is correct
7 Correct 8 ms 5224 KB Output is correct
8 Correct 9 ms 5120 KB Output is correct
9 Correct 8 ms 5148 KB Output is correct
10 Correct 8 ms 5168 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 7 ms 5348 KB Output is correct
14 Correct 7 ms 5220 KB Output is correct
15 Correct 7 ms 5292 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 7 ms 5248 KB Output is correct
18 Correct 7 ms 5348 KB Output is correct
19 Correct 8 ms 5252 KB Output is correct
20 Correct 10 ms 5348 KB Output is correct
21 Correct 7 ms 5256 KB Output is correct
22 Correct 8 ms 5240 KB Output is correct
23 Correct 8 ms 5220 KB Output is correct
24 Correct 26 ms 5248 KB Output is correct
25 Correct 8 ms 5168 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 8 ms 5344 KB Output is correct
28 Correct 7 ms 5376 KB Output is correct
29 Correct 7 ms 5248 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 7 ms 5376 KB Output is correct
32 Correct 7 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 298 ms 13028 KB Output is correct
2 Correct 385 ms 23440 KB Output is correct
3 Correct 45 ms 8700 KB Output is correct
4 Correct 602 ms 21788 KB Output is correct
5 Correct 702 ms 40712 KB Output is correct
6 Correct 640 ms 30724 KB Output is correct
7 Correct 564 ms 21884 KB Output is correct
8 Correct 633 ms 24096 KB Output is correct
9 Correct 624 ms 23232 KB Output is correct
10 Correct 604 ms 23116 KB Output is correct
11 Correct 366 ms 22460 KB Output is correct
12 Correct 690 ms 32616 KB Output is correct
13 Correct 586 ms 30248 KB Output is correct
14 Correct 628 ms 30332 KB Output is correct
15 Correct 340 ms 22368 KB Output is correct
16 Correct 601 ms 34748 KB Output is correct
17 Correct 601 ms 30968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 16596 KB Output is correct
2 Correct 704 ms 40768 KB Output is correct
3 Correct 48 ms 9408 KB Output is correct
4 Correct 662 ms 23488 KB Output is correct
5 Correct 739 ms 42308 KB Output is correct
6 Correct 695 ms 32420 KB Output is correct
7 Correct 632 ms 23492 KB Output is correct
8 Correct 711 ms 27380 KB Output is correct
9 Correct 600 ms 26244 KB Output is correct
10 Correct 650 ms 25104 KB Output is correct
11 Correct 445 ms 23724 KB Output is correct
12 Correct 701 ms 38576 KB Output is correct
13 Correct 655 ms 31432 KB Output is correct
14 Correct 674 ms 31656 KB Output is correct
15 Correct 395 ms 24164 KB Output is correct
16 Correct 641 ms 36280 KB Output is correct
17 Correct 723 ms 32572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5092 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 6 ms 5220 KB Output is correct
4 Correct 8 ms 5220 KB Output is correct
5 Correct 9 ms 5220 KB Output is correct
6 Correct 7 ms 5376 KB Output is correct
7 Correct 8 ms 5224 KB Output is correct
8 Correct 9 ms 5120 KB Output is correct
9 Correct 8 ms 5148 KB Output is correct
10 Correct 8 ms 5168 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 7 ms 5348 KB Output is correct
14 Correct 7 ms 5220 KB Output is correct
15 Correct 7 ms 5292 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 7 ms 5248 KB Output is correct
18 Correct 7 ms 5348 KB Output is correct
19 Correct 8 ms 5252 KB Output is correct
20 Correct 10 ms 5348 KB Output is correct
21 Correct 7 ms 5256 KB Output is correct
22 Correct 8 ms 5240 KB Output is correct
23 Correct 8 ms 5220 KB Output is correct
24 Correct 26 ms 5248 KB Output is correct
25 Correct 8 ms 5168 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 8 ms 5344 KB Output is correct
28 Correct 7 ms 5376 KB Output is correct
29 Correct 7 ms 5248 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 7 ms 5376 KB Output is correct
32 Correct 7 ms 5248 KB Output is correct
33 Correct 298 ms 13028 KB Output is correct
34 Correct 385 ms 23440 KB Output is correct
35 Correct 45 ms 8700 KB Output is correct
36 Correct 602 ms 21788 KB Output is correct
37 Correct 702 ms 40712 KB Output is correct
38 Correct 640 ms 30724 KB Output is correct
39 Correct 564 ms 21884 KB Output is correct
40 Correct 633 ms 24096 KB Output is correct
41 Correct 624 ms 23232 KB Output is correct
42 Correct 604 ms 23116 KB Output is correct
43 Correct 366 ms 22460 KB Output is correct
44 Correct 690 ms 32616 KB Output is correct
45 Correct 586 ms 30248 KB Output is correct
46 Correct 628 ms 30332 KB Output is correct
47 Correct 340 ms 22368 KB Output is correct
48 Correct 601 ms 34748 KB Output is correct
49 Correct 601 ms 30968 KB Output is correct
50 Correct 461 ms 16596 KB Output is correct
51 Correct 704 ms 40768 KB Output is correct
52 Correct 48 ms 9408 KB Output is correct
53 Correct 662 ms 23488 KB Output is correct
54 Correct 739 ms 42308 KB Output is correct
55 Correct 695 ms 32420 KB Output is correct
56 Correct 632 ms 23492 KB Output is correct
57 Correct 711 ms 27380 KB Output is correct
58 Correct 600 ms 26244 KB Output is correct
59 Correct 650 ms 25104 KB Output is correct
60 Correct 445 ms 23724 KB Output is correct
61 Correct 701 ms 38576 KB Output is correct
62 Correct 655 ms 31432 KB Output is correct
63 Correct 674 ms 31656 KB Output is correct
64 Correct 395 ms 24164 KB Output is correct
65 Correct 641 ms 36280 KB Output is correct
66 Correct 723 ms 32572 KB Output is correct
67 Correct 42 ms 7416 KB Output is correct
68 Correct 246 ms 20700 KB Output is correct
69 Correct 380 ms 21380 KB Output is correct
70 Correct 611 ms 21848 KB Output is correct
71 Correct 706 ms 40656 KB Output is correct
72 Correct 726 ms 30764 KB Output is correct
73 Correct 606 ms 21852 KB Output is correct
74 Correct 708 ms 25276 KB Output is correct
75 Correct 674 ms 23452 KB Output is correct
76 Correct 658 ms 23400 KB Output is correct
77 Correct 530 ms 22052 KB Output is correct
78 Correct 692 ms 35652 KB Output is correct
79 Correct 609 ms 32416 KB Output is correct
80 Correct 670 ms 29104 KB Output is correct
81 Correct 352 ms 22172 KB Output is correct
82 Correct 608 ms 34624 KB Output is correct
83 Correct 629 ms 30844 KB Output is correct
84 Correct 600 ms 22104 KB Output is correct
85 Correct 703 ms 40860 KB Output is correct
86 Correct 708 ms 31100 KB Output is correct
87 Correct 595 ms 22116 KB Output is correct
88 Correct 652 ms 25952 KB Output is correct
89 Correct 618 ms 24412 KB Output is correct
90 Correct 604 ms 23804 KB Output is correct
91 Correct 493 ms 22492 KB Output is correct
92 Correct 685 ms 40336 KB Output is correct
93 Correct 620 ms 28296 KB Output is correct
94 Correct 649 ms 26752 KB Output is correct
95 Correct 339 ms 22736 KB Output is correct
96 Correct 619 ms 34968 KB Output is correct
97 Correct 660 ms 31324 KB Output is correct
98 Correct 644 ms 23520 KB Output is correct
99 Correct 725 ms 41316 KB Output is correct
100 Correct 697 ms 32080 KB Output is correct
101 Correct 583 ms 22720 KB Output is correct
102 Correct 667 ms 26060 KB Output is correct
103 Correct 657 ms 24588 KB Output is correct
104 Correct 629 ms 24312 KB Output is correct
105 Correct 508 ms 22920 KB Output is correct
106 Correct 698 ms 33104 KB Output is correct
107 Correct 594 ms 33244 KB Output is correct
108 Correct 685 ms 28992 KB Output is correct
109 Correct 358 ms 23632 KB Output is correct
110 Correct 618 ms 35536 KB Output is correct
111 Correct 639 ms 32012 KB Output is correct