답안 #99740

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
99740 2019-03-06T11:19:33 Z cki86201 Unique Cities (JOI19_ho_t5) C++11
100 / 100
1826 ms 70968 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <limits.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
typedef tuple<int, int, int> t3;

int N, M;
vector <int> E[200020];

int dis[2][200020];
int far(int t, int x) {
	vector <int> q;
	q.pb(x);
	for(int i=1;i<=N;i++) dis[t][i] = -1;
	dis[t][x] = 0;
	rep(i, szz(q)) {
		int a = q[i];
		for(int e : E[a]) if(dis[t][e] == -1) {
			dis[t][e] = dis[t][a] + 1;
			q.pb(e);
		}
	}
	return q.back();
}

int ans[200020];
int dep[200020], mxdep[200020];
int ST[200020], EN[200020], cs, rev[200020];
int par[200020];
int Q[200020];
int nxt[200020][20];

void pdfs(int x, int fa) {
	mxdep[x] = dep[x];
	ST[x] = ++cs; rev[cs] = x;
	int mxl[2] = {};
	for(int e : E[x]) if(e != fa) {
		dep[e] = dep[x] + 1;
		par[e] = x;
		pdfs(e, x);
		if(mxdep[x] < mxdep[e]) mxdep[x] = mxdep[e], nxt[x][0] = e;
		int L = mxdep[e] - dep[x];
		if(mxl[0] < L) mxl[1] = mxl[0], mxl[0] = L;
		else if(mxl[1] < L) mxl[1] = L;
	}
	int len = mxl[1];
	if(len) {
		int t = x;
		rep(j, len) Q[t] = 1, t = par[t];
	}
	EN[x] = cs;
}

int get_nxt(int x, int p) {
	rep(i, 20) if(1<<i & p) x = nxt[x][i];
	return x;
}

int C[200020];
vector <int> H[200020];
int cnt[200020];

int main() {
	scanf("%d%d", &N, &M);
	for(int i=1;i<N;i++) {
		int x, y; scanf("%d%d", &x, &y);
		E[x].pb(y); E[y].pb(x);
	}
	for(int i=1;i<=N;i++) scanf("%d", C + i);
	int a = far(0, 1);
	int b = far(0, a);
	far(1, b);
	int tt = 0;
	int X[2][200020] = {};
	for(int rt : {a, b}) {
		cs = 0;
		for(int i=1;i<=N;i++) {
			Q[i] = 0;
			rep(j, 20) nxt[i][j] = 0;
			H[i].clear();
		}
		dep[rt] = 0;
		pdfs(rt, -1);
		for(int j=1;j<20;j++) {
			for(int i=1;i<=N;i++) nxt[i][j] = nxt[ nxt[i][j-1] ][j-1];
		}
		for(int i=1;i<=N;i++) if(Q[i] == 0 && i != rt) {
			int L = mxdep[i] - dep[i];
			int ni = get_nxt(i, (L + 1) / 2);
			H[ST[ni]].pb(C[par[i]]);
			H[EN[ni]+1].pb(-C[par[i]]);
		}
		for(int i=1;i<=M;i++) cnt[i] = 0;
		int res = 0;
		for(int i=1;i<=N;i++) {
			for(int e : H[i]) {
				if(e > 0) res += (cnt[e] == 0), ++cnt[e];
				else res -= (cnt[-e] == 1), --cnt[-e];
			}
			X[tt][rev[i]] = res;
		}
		++tt;
	}
	for(int i=1;i<=N;i++) {
		if(dis[0][i] >= dis[1][i]) ans[i] = X[0][i];
		else ans[i] = X[1][i];
	}
	for(int i=1;i<=N;i++) printf("%d\n", ans[i]);
	
	return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:89: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:91:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y; scanf("%d%d", &x, &y);
             ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:94:29: 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", C + i);
                        ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11392 KB Output is correct
2 Correct 16 ms 11648 KB Output is correct
3 Correct 16 ms 11648 KB Output is correct
4 Correct 14 ms 11776 KB Output is correct
5 Correct 15 ms 11648 KB Output is correct
6 Correct 16 ms 11904 KB Output is correct
7 Correct 17 ms 11776 KB Output is correct
8 Correct 19 ms 11776 KB Output is correct
9 Correct 16 ms 11776 KB Output is correct
10 Correct 17 ms 11776 KB Output is correct
11 Correct 17 ms 11776 KB Output is correct
12 Correct 17 ms 11776 KB Output is correct
13 Correct 14 ms 11904 KB Output is correct
14 Correct 16 ms 11776 KB Output is correct
15 Correct 16 ms 11748 KB Output is correct
16 Correct 16 ms 11776 KB Output is correct
17 Correct 16 ms 11776 KB Output is correct
18 Correct 16 ms 11776 KB Output is correct
19 Correct 17 ms 11648 KB Output is correct
20 Correct 16 ms 11904 KB Output is correct
21 Correct 16 ms 11776 KB Output is correct
22 Correct 20 ms 11648 KB Output is correct
23 Correct 18 ms 11748 KB Output is correct
24 Correct 16 ms 11776 KB Output is correct
25 Correct 16 ms 11776 KB Output is correct
26 Correct 18 ms 11776 KB Output is correct
27 Correct 19 ms 11904 KB Output is correct
28 Correct 15 ms 11776 KB Output is correct
29 Correct 18 ms 11904 KB Output is correct
30 Correct 15 ms 11696 KB Output is correct
31 Correct 17 ms 11648 KB Output is correct
32 Correct 18 ms 11776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 33416 KB Output is correct
2 Correct 806 ms 44152 KB Output is correct
3 Correct 107 ms 17812 KB Output is correct
4 Correct 913 ms 49424 KB Output is correct
5 Correct 1640 ms 67948 KB Output is correct
6 Correct 1290 ms 56912 KB Output is correct
7 Correct 923 ms 49832 KB Output is correct
8 Correct 822 ms 51324 KB Output is correct
9 Correct 973 ms 50852 KB Output is correct
10 Correct 966 ms 50900 KB Output is correct
11 Correct 721 ms 50472 KB Output is correct
12 Correct 1102 ms 60724 KB Output is correct
13 Correct 1272 ms 58536 KB Output is correct
14 Correct 1178 ms 57156 KB Output is correct
15 Correct 683 ms 50424 KB Output is correct
16 Correct 1287 ms 61932 KB Output is correct
17 Correct 1160 ms 57420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 737 ms 41712 KB Output is correct
2 Correct 1619 ms 66576 KB Output is correct
3 Correct 157 ms 18864 KB Output is correct
4 Correct 989 ms 49496 KB Output is correct
5 Correct 1734 ms 68980 KB Output is correct
6 Correct 1234 ms 56700 KB Output is correct
7 Correct 951 ms 49648 KB Output is correct
8 Correct 1131 ms 53540 KB Output is correct
9 Correct 1117 ms 52080 KB Output is correct
10 Correct 1025 ms 51100 KB Output is correct
11 Correct 864 ms 49948 KB Output is correct
12 Correct 1474 ms 64496 KB Output is correct
13 Correct 1151 ms 57496 KB Output is correct
14 Correct 1153 ms 56880 KB Output is correct
15 Correct 645 ms 50308 KB Output is correct
16 Correct 1352 ms 63344 KB Output is correct
17 Correct 1281 ms 57340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 11392 KB Output is correct
2 Correct 16 ms 11648 KB Output is correct
3 Correct 16 ms 11648 KB Output is correct
4 Correct 14 ms 11776 KB Output is correct
5 Correct 15 ms 11648 KB Output is correct
6 Correct 16 ms 11904 KB Output is correct
7 Correct 17 ms 11776 KB Output is correct
8 Correct 19 ms 11776 KB Output is correct
9 Correct 16 ms 11776 KB Output is correct
10 Correct 17 ms 11776 KB Output is correct
11 Correct 17 ms 11776 KB Output is correct
12 Correct 17 ms 11776 KB Output is correct
13 Correct 14 ms 11904 KB Output is correct
14 Correct 16 ms 11776 KB Output is correct
15 Correct 16 ms 11748 KB Output is correct
16 Correct 16 ms 11776 KB Output is correct
17 Correct 16 ms 11776 KB Output is correct
18 Correct 16 ms 11776 KB Output is correct
19 Correct 17 ms 11648 KB Output is correct
20 Correct 16 ms 11904 KB Output is correct
21 Correct 16 ms 11776 KB Output is correct
22 Correct 20 ms 11648 KB Output is correct
23 Correct 18 ms 11748 KB Output is correct
24 Correct 16 ms 11776 KB Output is correct
25 Correct 16 ms 11776 KB Output is correct
26 Correct 18 ms 11776 KB Output is correct
27 Correct 19 ms 11904 KB Output is correct
28 Correct 15 ms 11776 KB Output is correct
29 Correct 18 ms 11904 KB Output is correct
30 Correct 15 ms 11696 KB Output is correct
31 Correct 17 ms 11648 KB Output is correct
32 Correct 18 ms 11776 KB Output is correct
33 Correct 456 ms 33416 KB Output is correct
34 Correct 806 ms 44152 KB Output is correct
35 Correct 107 ms 17812 KB Output is correct
36 Correct 913 ms 49424 KB Output is correct
37 Correct 1640 ms 67948 KB Output is correct
38 Correct 1290 ms 56912 KB Output is correct
39 Correct 923 ms 49832 KB Output is correct
40 Correct 822 ms 51324 KB Output is correct
41 Correct 973 ms 50852 KB Output is correct
42 Correct 966 ms 50900 KB Output is correct
43 Correct 721 ms 50472 KB Output is correct
44 Correct 1102 ms 60724 KB Output is correct
45 Correct 1272 ms 58536 KB Output is correct
46 Correct 1178 ms 57156 KB Output is correct
47 Correct 683 ms 50424 KB Output is correct
48 Correct 1287 ms 61932 KB Output is correct
49 Correct 1160 ms 57420 KB Output is correct
50 Correct 737 ms 41712 KB Output is correct
51 Correct 1619 ms 66576 KB Output is correct
52 Correct 157 ms 18864 KB Output is correct
53 Correct 989 ms 49496 KB Output is correct
54 Correct 1734 ms 68980 KB Output is correct
55 Correct 1234 ms 56700 KB Output is correct
56 Correct 951 ms 49648 KB Output is correct
57 Correct 1131 ms 53540 KB Output is correct
58 Correct 1117 ms 52080 KB Output is correct
59 Correct 1025 ms 51100 KB Output is correct
60 Correct 864 ms 49948 KB Output is correct
61 Correct 1474 ms 64496 KB Output is correct
62 Correct 1151 ms 57496 KB Output is correct
63 Correct 1153 ms 56880 KB Output is correct
64 Correct 645 ms 50308 KB Output is correct
65 Correct 1352 ms 63344 KB Output is correct
66 Correct 1281 ms 57340 KB Output is correct
67 Correct 77 ms 16940 KB Output is correct
68 Correct 597 ms 37508 KB Output is correct
69 Correct 653 ms 41000 KB Output is correct
70 Correct 1014 ms 51340 KB Output is correct
71 Correct 1775 ms 69924 KB Output is correct
72 Correct 1218 ms 57908 KB Output is correct
73 Correct 902 ms 51484 KB Output is correct
74 Correct 1042 ms 54940 KB Output is correct
75 Correct 988 ms 53028 KB Output is correct
76 Correct 983 ms 52880 KB Output is correct
77 Correct 881 ms 52008 KB Output is correct
78 Correct 1286 ms 64452 KB Output is correct
79 Correct 1307 ms 62676 KB Output is correct
80 Correct 1065 ms 58284 KB Output is correct
81 Correct 670 ms 52140 KB Output is correct
82 Correct 1420 ms 64436 KB Output is correct
83 Correct 1170 ms 59272 KB Output is correct
84 Correct 816 ms 51628 KB Output is correct
85 Correct 1630 ms 70616 KB Output is correct
86 Correct 1270 ms 58452 KB Output is correct
87 Correct 956 ms 51796 KB Output is correct
88 Correct 994 ms 55432 KB Output is correct
89 Correct 857 ms 53788 KB Output is correct
90 Correct 1041 ms 53264 KB Output is correct
91 Correct 987 ms 52560 KB Output is correct
92 Correct 1689 ms 69212 KB Output is correct
93 Correct 1099 ms 58084 KB Output is correct
94 Correct 992 ms 56196 KB Output is correct
95 Correct 569 ms 52416 KB Output is correct
96 Correct 1367 ms 64908 KB Output is correct
97 Correct 1234 ms 59832 KB Output is correct
98 Correct 1000 ms 52984 KB Output is correct
99 Correct 1826 ms 70968 KB Output is correct
100 Correct 1331 ms 60004 KB Output is correct
101 Correct 907 ms 52408 KB Output is correct
102 Correct 938 ms 55332 KB Output is correct
103 Correct 927 ms 54224 KB Output is correct
104 Correct 992 ms 53672 KB Output is correct
105 Correct 904 ms 52608 KB Output is correct
106 Correct 1262 ms 63012 KB Output is correct
107 Correct 1263 ms 63408 KB Output is correct
108 Correct 1010 ms 58360 KB Output is correct
109 Correct 689 ms 53536 KB Output is correct
110 Correct 1352 ms 65848 KB Output is correct
111 Correct 1060 ms 60820 KB Output is correct