답안 #124706

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124706 2019-07-03T17:56:40 Z duality Unique Cities (JOI19_ho_t5) C++11
100 / 100
482 ms 36844 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

vi adjList[200000];
int C[200000];
int depth[200000],height[200000];
int doDFS(int u,int p,int d) {
    int i;
    depth[u] = d,height[u] = 0;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) height[u] = max(height[u],doDFS(v,u,d+1)+1);
    }
    return height[u];
}
vi path;
int c[200000],n = 0;
int push(int x) {
    path.pb(x),c[C[x]]++;
    if (c[C[x]] == 1) n++;
    return 0;
}
int pop() {
    c[C[path.back()]]--;
    if (c[C[path.back()]] == 0) n--;
    path.pop_back();
    return 0;
}
int ans[200000];
int doDFS2(int u,int p) {
    int i;
    int m1 = 0,m2 = 0,mi = -1;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            if (height[v]+1 >= m1) m2 = m1,m1 = height[v]+1,mi = i;
            else if (height[v]+1 >= m2) m2 = height[v]+1;
        }
    }
    if (mi != -1) swap(adjList[u][0],adjList[u][mi]);
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            while (!path.empty() && (depth[path.back()] >= depth[u]-((i == 0) ? m2:m1))) pop();
            push(u),doDFS2(v,u);
            if (!path.empty() && (path.back() == u)) pop();
        }
    }
    while (!path.empty() && (depth[path.back()] >= depth[u]-m1)) pop();
    ans[u] = max(ans[u],n);
    return 0;
}
int main() {
    int i;
    int N,M,A,B;
    scanf("%d %d",&N,&M);
    for (i = 0; i < N-1; i++) {
        scanf("%d %d",&A,&B);
        A--,B--;
        adjList[A].pb(B);
        adjList[B].pb(A);
    }
    for (i = 0; i < N; i++) scanf("%d",&C[i]),C[i]--;

    A = B = 0;
    doDFS(0,-1,0);
    for (i = 0; i < N; i++) {
        if (depth[i] > depth[A]) A = i;
    }
    doDFS(A,-1,0),doDFS2(A,-1);
    for (i = 0; i < N; i++) {
        if (depth[i] > depth[B]) B = i;
    }
    doDFS(B,-1,0),doDFS2(B,-1);
    for (i = 0; i < N; i++) printf("%d\n",ans[i]);

    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'int doDFS(int, int, int)':
joi2019_ho_t5.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int doDFS2(int, int)':
joi2019_ho_t5.cpp:87:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:95:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:110:10: 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:112:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&A,&B);
         ~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:117:46: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&C[i]),C[i]--;
                             ~~~~~~~~~~~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 8 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 7 ms 5240 KB Output is correct
8 Correct 7 ms 5240 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5244 KB Output is correct
11 Correct 8 ms 5244 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5240 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 8 ms 5368 KB Output is correct
21 Correct 8 ms 5240 KB Output is correct
22 Correct 8 ms 5084 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 8 ms 5112 KB Output is correct
25 Correct 8 ms 5240 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 9 ms 5368 KB Output is correct
28 Correct 7 ms 5208 KB Output is correct
29 Correct 8 ms 5240 KB Output is correct
30 Correct 7 ms 5160 KB Output is correct
31 Correct 9 ms 5368 KB Output is correct
32 Correct 8 ms 5240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 140 ms 12280 KB Output is correct
2 Correct 181 ms 21820 KB Output is correct
3 Correct 36 ms 7928 KB Output is correct
4 Correct 339 ms 17944 KB Output is correct
5 Correct 391 ms 34284 KB Output is correct
6 Correct 364 ms 26032 KB Output is correct
7 Correct 317 ms 17948 KB Output is correct
8 Correct 345 ms 19448 KB Output is correct
9 Correct 437 ms 19064 KB Output is correct
10 Correct 327 ms 18816 KB Output is correct
11 Correct 171 ms 18536 KB Output is correct
12 Correct 338 ms 27892 KB Output is correct
13 Correct 384 ms 25836 KB Output is correct
14 Correct 338 ms 25304 KB Output is correct
15 Correct 176 ms 18508 KB Output is correct
16 Correct 350 ms 29268 KB Output is correct
17 Correct 346 ms 26228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 231 ms 16452 KB Output is correct
2 Correct 421 ms 35436 KB Output is correct
3 Correct 42 ms 8568 KB Output is correct
4 Correct 455 ms 19492 KB Output is correct
5 Correct 431 ms 36844 KB Output is correct
6 Correct 383 ms 27892 KB Output is correct
7 Correct 310 ms 19576 KB Output is correct
8 Correct 392 ms 23032 KB Output is correct
9 Correct 366 ms 21880 KB Output is correct
10 Correct 378 ms 20728 KB Output is correct
11 Correct 287 ms 19824 KB Output is correct
12 Correct 423 ms 33392 KB Output is correct
13 Correct 361 ms 26868 KB Output is correct
14 Correct 472 ms 26992 KB Output is correct
15 Correct 186 ms 19436 KB Output is correct
16 Correct 474 ms 31488 KB Output is correct
17 Correct 363 ms 28016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 8 ms 5240 KB Output is correct
3 Correct 7 ms 5240 KB Output is correct
4 Correct 8 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 7 ms 5240 KB Output is correct
8 Correct 7 ms 5240 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5244 KB Output is correct
11 Correct 8 ms 5244 KB Output is correct
12 Correct 7 ms 5248 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 8 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5240 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 8 ms 5368 KB Output is correct
21 Correct 8 ms 5240 KB Output is correct
22 Correct 8 ms 5084 KB Output is correct
23 Correct 8 ms 5240 KB Output is correct
24 Correct 8 ms 5112 KB Output is correct
25 Correct 8 ms 5240 KB Output is correct
26 Correct 8 ms 5240 KB Output is correct
27 Correct 9 ms 5368 KB Output is correct
28 Correct 7 ms 5208 KB Output is correct
29 Correct 8 ms 5240 KB Output is correct
30 Correct 7 ms 5160 KB Output is correct
31 Correct 9 ms 5368 KB Output is correct
32 Correct 8 ms 5240 KB Output is correct
33 Correct 140 ms 12280 KB Output is correct
34 Correct 181 ms 21820 KB Output is correct
35 Correct 36 ms 7928 KB Output is correct
36 Correct 339 ms 17944 KB Output is correct
37 Correct 391 ms 34284 KB Output is correct
38 Correct 364 ms 26032 KB Output is correct
39 Correct 317 ms 17948 KB Output is correct
40 Correct 345 ms 19448 KB Output is correct
41 Correct 437 ms 19064 KB Output is correct
42 Correct 327 ms 18816 KB Output is correct
43 Correct 171 ms 18536 KB Output is correct
44 Correct 338 ms 27892 KB Output is correct
45 Correct 384 ms 25836 KB Output is correct
46 Correct 338 ms 25304 KB Output is correct
47 Correct 176 ms 18508 KB Output is correct
48 Correct 350 ms 29268 KB Output is correct
49 Correct 346 ms 26228 KB Output is correct
50 Correct 231 ms 16452 KB Output is correct
51 Correct 421 ms 35436 KB Output is correct
52 Correct 42 ms 8568 KB Output is correct
53 Correct 455 ms 19492 KB Output is correct
54 Correct 431 ms 36844 KB Output is correct
55 Correct 383 ms 27892 KB Output is correct
56 Correct 310 ms 19576 KB Output is correct
57 Correct 392 ms 23032 KB Output is correct
58 Correct 366 ms 21880 KB Output is correct
59 Correct 378 ms 20728 KB Output is correct
60 Correct 287 ms 19824 KB Output is correct
61 Correct 423 ms 33392 KB Output is correct
62 Correct 361 ms 26868 KB Output is correct
63 Correct 472 ms 26992 KB Output is correct
64 Correct 186 ms 19436 KB Output is correct
65 Correct 474 ms 31488 KB Output is correct
66 Correct 363 ms 28016 KB Output is correct
67 Correct 35 ms 6904 KB Output is correct
68 Correct 137 ms 18504 KB Output is correct
69 Correct 293 ms 18608 KB Output is correct
70 Correct 444 ms 17912 KB Output is correct
71 Correct 482 ms 34380 KB Output is correct
72 Correct 424 ms 25976 KB Output is correct
73 Correct 343 ms 17912 KB Output is correct
74 Correct 376 ms 21008 KB Output is correct
75 Correct 344 ms 19292 KB Output is correct
76 Correct 379 ms 19320 KB Output is correct
77 Correct 257 ms 18128 KB Output is correct
78 Correct 423 ms 29884 KB Output is correct
79 Correct 449 ms 27996 KB Output is correct
80 Correct 434 ms 24436 KB Output is correct
81 Correct 191 ms 18376 KB Output is correct
82 Correct 384 ms 29032 KB Output is correct
83 Correct 386 ms 26108 KB Output is correct
84 Correct 355 ms 18168 KB Output is correct
85 Correct 469 ms 35004 KB Output is correct
86 Correct 409 ms 26488 KB Output is correct
87 Correct 348 ms 18252 KB Output is correct
88 Correct 351 ms 21532 KB Output is correct
89 Correct 407 ms 20068 KB Output is correct
90 Correct 401 ms 19448 KB Output is correct
91 Correct 314 ms 18728 KB Output is correct
92 Correct 468 ms 34516 KB Output is correct
93 Correct 360 ms 24144 KB Output is correct
94 Correct 339 ms 22392 KB Output is correct
95 Correct 176 ms 18920 KB Output is correct
96 Correct 353 ms 29656 KB Output is correct
97 Correct 361 ms 26612 KB Output is correct
98 Correct 311 ms 19480 KB Output is correct
99 Correct 420 ms 35496 KB Output is correct
100 Correct 387 ms 27612 KB Output is correct
101 Correct 305 ms 18800 KB Output is correct
102 Correct 349 ms 21664 KB Output is correct
103 Correct 328 ms 20472 KB Output is correct
104 Correct 318 ms 20164 KB Output is correct
105 Correct 253 ms 18896 KB Output is correct
106 Correct 409 ms 28624 KB Output is correct
107 Correct 339 ms 28836 KB Output is correct
108 Correct 342 ms 24560 KB Output is correct
109 Correct 189 ms 19532 KB Output is correct
110 Correct 356 ms 30424 KB Output is correct
111 Correct 440 ms 27728 KB Output is correct