답안 #905295

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
905295 2024-01-12T23:07:12 Z jay_jayjay Unique Cities (JOI19_ho_t5) C++17
100 / 100
685 ms 71508 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f
#define all(x) (x).begin(),(x).end()

int main() {
        int n,m;scanf("%d%d",&n,&m);

        vector<vector<int>> adj(n);
        for(int i=1;i<n;i++) {
                int u,v;scanf("%d%d",&u,&v);u--;v--;
                adj[u].push_back(v); adj[v].push_back(u);
        }

        vector<int> c(n);for(auto&x:c)scanf("%d",&x),x--;

        // ;-;
        vector<int> sz(n),dep(n),mxdep(n),val(n);
        auto dfs1 = [&](auto self, int v, int p=-1, int rk=0)  -> void {
                sz[v]=1; dep[v]=rk; mxdep[v]=0;
                for(auto x:adj[v]) if(x!=p)
                        self(self,x,v,rk+1),
                                sz[v]+=sz[x],
                                mxdep[v]=max(mxdep[v],mxdep[x]+1);
        };

        vector<int> S(m); int cnt=0;
        auto add = [&](int x, int d) {
                 //printf("\t\tadd %d %d\n",x,d);
                cnt-=S[x]>0;
                S[x]+=d;
                cnt+=S[x]>0;
        };
        vector<int> A;
        vector<array<int,2>> B;
        auto dfs2 = [&](auto self, int v, int p=-1) -> void {
                sort(all(adj[v]), [&](int a, int b) { return mxdep[a] > mxdep[b]; });
                assert(p == -1 || adj[v][0] == p);
                if(p==-1) adj[v].insert(adj[v].begin(),-1);

                //printf("%d: ", v);
                //for(auto x:A)printf("%d ",x);
                //printf("\n");
                //for(auto x:B)printf("%d|%d ", x[0],x[1]);
                //printf("\n");

                vector<array<int,2>> R;
                for(int i=1;i<adj[v].size();i++) {
                        int x = adj[v][i];

                        int d=0;
                        if(i > 1) d=mxdep[adj[v][1]]+1;
                        else if(adj[v].size() > 2) d=mxdep[adj[v][2]]+1;
                         //printf("\t\t\tv=%d x=%d d=%d\n",v,x,d);
                         //printf("\t\t\tcut %d\n", dep[v]-d);

                        while(B.size() && B.back()[0] >= dep[v] - d) {
                                R.push_back(B.back());
                                add(B.back()[1], -1);
                                B.pop_back();
                        }
                        B.push_back({dep[v], c[v]});
                        add(c[v],1);
                        A.push_back(d);

                        self(self, x, v);

                        A.pop_back();
                        if(B.size() && B.back()[0]==dep[v]) B.pop_back(), add(c[v],-1);
                }
                if(adj[v].size()>1) {
                        int d=mxdep[adj[v][1]]+1;
                        // printf("\t\t\texit d=%d\n", adj[v][1]);
                        while(B.size() && B.back()[0] >= dep[v] - d) {
                                R.push_back(B.back());
                                add(B.back()[1], -1);
                                B.pop_back();
                        }
                }
                //printf("%d exit: ", v);
                //for(auto x:A)printf("%d ",x);
                //printf("(%d)\n", adj[v].size()>1?adj[v][1]:0);
                //for(auto x:B)printf("%d|%d ", x[0],x[1]);
                //printf("\n");
                //for(int i=0;i<m;i++)printf("%d ",S[i]);
                //printf("\n");
                val[v] = max(val[v],cnt);
                //while(R.size()) {
                //B.push_back(R.back());
                //add(B.back()[1], 1);
                //R.pop_back();
                //}
                if(p==-1)adj[v].erase(adj[v].begin());
        };

        // find dia
        dfs1(dfs1, 0);
        int v1 = max_element(all(dep)) - dep.begin();
        dfs1(dfs1, v1);
        int v2 = max_element(all(dep)) - dep.begin();

        // printf("dia is %d, %d\n", v1, v2);

        dfs2(dfs2,v1);
        //for(int i=0;i<n;i++)printf("%d ",val[i]);
        //printf("\n");
        dfs1(dfs1,v2);
        dfs2(dfs2,v2);
        for(int i=0;i<n;i++)printf("%d ",val[i]);
        printf("\n");
}

Compilation message

joi2019_ho_t5.cpp: In instantiation of 'main()::<lambda(auto:24, int, int)> [with auto:24 = main()::<lambda(auto:24, int, int)>]':
joi2019_ho_t5.cpp:106:21:   required from here
joi2019_ho_t5.cpp:50:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |                 for(int i=1;i<adj[v].size();i++) {
      |                             ~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:9:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |         int n,m;scanf("%d%d",&n,&m);
      |                 ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:13:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |                 int u,v;scanf("%d%d",&u,&v);u--;v--;
      |                         ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:17:44: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |         vector<int> c(n);for(auto&x:c)scanf("%d",&x),x--;
      |                                       ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 828 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 1080 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 600 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 2 ms 856 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 2 ms 700 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 3 ms 860 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 218 ms 9300 KB Output is correct
2 Correct 203 ms 38948 KB Output is correct
3 Correct 26 ms 6492 KB Output is correct
4 Correct 282 ms 18948 KB Output is correct
5 Correct 685 ms 68884 KB Output is correct
6 Correct 449 ms 43416 KB Output is correct
7 Correct 221 ms 18664 KB Output is correct
8 Correct 308 ms 24008 KB Output is correct
9 Correct 254 ms 22264 KB Output is correct
10 Correct 310 ms 22152 KB Output is correct
11 Correct 141 ms 19316 KB Output is correct
12 Correct 498 ms 50268 KB Output is correct
13 Correct 354 ms 43024 KB Output is correct
14 Correct 507 ms 42080 KB Output is correct
15 Correct 121 ms 19400 KB Output is correct
16 Correct 454 ms 51868 KB Output is correct
17 Correct 396 ms 43460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 13140 KB Output is correct
2 Correct 581 ms 66644 KB Output is correct
3 Correct 24 ms 7508 KB Output is correct
4 Correct 326 ms 20344 KB Output is correct
5 Correct 609 ms 71508 KB Output is correct
6 Correct 430 ms 45056 KB Output is correct
7 Correct 222 ms 20404 KB Output is correct
8 Correct 406 ms 31056 KB Output is correct
9 Correct 394 ms 27224 KB Output is correct
10 Correct 312 ms 24408 KB Output is correct
11 Correct 223 ms 20604 KB Output is correct
12 Correct 517 ms 60952 KB Output is correct
13 Correct 386 ms 41924 KB Output is correct
14 Correct 415 ms 42220 KB Output is correct
15 Correct 139 ms 20988 KB Output is correct
16 Correct 468 ms 54024 KB Output is correct
17 Correct 356 ms 44996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 828 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 1080 KB Output is correct
21 Correct 2 ms 860 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 600 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 2 ms 856 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 2 ms 700 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 3 ms 860 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
33 Correct 218 ms 9300 KB Output is correct
34 Correct 203 ms 38948 KB Output is correct
35 Correct 26 ms 6492 KB Output is correct
36 Correct 282 ms 18948 KB Output is correct
37 Correct 685 ms 68884 KB Output is correct
38 Correct 449 ms 43416 KB Output is correct
39 Correct 221 ms 18664 KB Output is correct
40 Correct 308 ms 24008 KB Output is correct
41 Correct 254 ms 22264 KB Output is correct
42 Correct 310 ms 22152 KB Output is correct
43 Correct 141 ms 19316 KB Output is correct
44 Correct 498 ms 50268 KB Output is correct
45 Correct 354 ms 43024 KB Output is correct
46 Correct 507 ms 42080 KB Output is correct
47 Correct 121 ms 19400 KB Output is correct
48 Correct 454 ms 51868 KB Output is correct
49 Correct 396 ms 43460 KB Output is correct
50 Correct 155 ms 13140 KB Output is correct
51 Correct 581 ms 66644 KB Output is correct
52 Correct 24 ms 7508 KB Output is correct
53 Correct 326 ms 20344 KB Output is correct
54 Correct 609 ms 71508 KB Output is correct
55 Correct 430 ms 45056 KB Output is correct
56 Correct 222 ms 20404 KB Output is correct
57 Correct 406 ms 31056 KB Output is correct
58 Correct 394 ms 27224 KB Output is correct
59 Correct 312 ms 24408 KB Output is correct
60 Correct 223 ms 20604 KB Output is correct
61 Correct 517 ms 60952 KB Output is correct
62 Correct 386 ms 41924 KB Output is correct
63 Correct 415 ms 42220 KB Output is correct
64 Correct 139 ms 20988 KB Output is correct
65 Correct 468 ms 54024 KB Output is correct
66 Correct 356 ms 44996 KB Output is correct
67 Correct 26 ms 3060 KB Output is correct
68 Correct 126 ms 30664 KB Output is correct
69 Correct 150 ms 27436 KB Output is correct
70 Correct 284 ms 18880 KB Output is correct
71 Correct 535 ms 68852 KB Output is correct
72 Correct 539 ms 43200 KB Output is correct
73 Correct 195 ms 18668 KB Output is correct
74 Correct 342 ms 28792 KB Output is correct
75 Correct 263 ms 23380 KB Output is correct
76 Correct 348 ms 22752 KB Output is correct
77 Correct 205 ms 19016 KB Output is correct
78 Correct 450 ms 55416 KB Output is correct
79 Correct 474 ms 48616 KB Output is correct
80 Correct 450 ms 38932 KB Output is correct
81 Correct 143 ms 19032 KB Output is correct
82 Correct 356 ms 51892 KB Output is correct
83 Correct 438 ms 43716 KB Output is correct
84 Correct 207 ms 19028 KB Output is correct
85 Correct 514 ms 69832 KB Output is correct
86 Correct 349 ms 43728 KB Output is correct
87 Correct 406 ms 18996 KB Output is correct
88 Correct 427 ms 29648 KB Output is correct
89 Correct 273 ms 24404 KB Output is correct
90 Correct 402 ms 23124 KB Output is correct
91 Correct 171 ms 19284 KB Output is correct
92 Correct 537 ms 67304 KB Output is correct
93 Correct 332 ms 36556 KB Output is correct
94 Correct 389 ms 32208 KB Output is correct
95 Correct 115 ms 19908 KB Output is correct
96 Correct 442 ms 52356 KB Output is correct
97 Correct 482 ms 43940 KB Output is correct
98 Correct 278 ms 20348 KB Output is correct
99 Correct 578 ms 70084 KB Output is correct
100 Correct 438 ms 44620 KB Output is correct
101 Correct 360 ms 19664 KB Output is correct
102 Correct 328 ms 28148 KB Output is correct
103 Correct 471 ms 24472 KB Output is correct
104 Correct 270 ms 23916 KB Output is correct
105 Correct 166 ms 19692 KB Output is correct
106 Correct 488 ms 48452 KB Output is correct
107 Correct 480 ms 48848 KB Output is correct
108 Correct 342 ms 35584 KB Output is correct
109 Correct 119 ms 20516 KB Output is correct
110 Correct 461 ms 53196 KB Output is correct
111 Correct 337 ms 44712 KB Output is correct