답안 #707765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
707765 2023-03-10T05:04:55 Z victor_gao Mergers (JOI19_mergers) C++17
100 / 100
689 ms 111320 KB
//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 500015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
struct DSU{
    int boss[N],sz[N],group=0;
    void init(int n){
        group=n;
        for (int i=0;i<=n+1;i++){
            boss[i]=i;
            sz[i]=1;
        }
    }
    int find(int x){
        if (boss[x]==x) return x;
        return boss[x]=find(boss[x]);
    }
    void Merge(int a,int b){
        int ra=find(a),rb=find(b);
        if (ra==rb) return;
        group--;
        if (sz[ra]<sz[rb]) swap(ra,rb);
        sz[ra]+=sz[rb];
        boss[rb]=ra;
    }
}d;
int n,k,fa[N],dep[N],is[N],deg[N],cnt=0;
vector<int>g[N],all[N];
void dfs(int p,int lp){
    fa[p]=lp; dep[p]=dep[lp]+1;
    for (auto i:g[p]){
        if (i!=lp)
            dfs(i,p);
    }
}
void merge_path(int a,int b){
    vector<int>path;
  //  cout<<"Go "<<a<<" "<<b<<'\n';
    while (a!=b){
        if (dep[a]>=dep[b]){
            path.push_back(a);
            d.Merge(is[a],is[fa[a]]);
            a=fa[a];
        }
        else {
            path.push_back(b);
            d.Merge(is[b],is[fa[b]]);
            b=fa[b];
        }
    }
    for (auto i:path)
        fa[i]=a;
}
signed main(){
    fast
    cin>>n>>k;
    d.init(k);
    vector<pii>edge;
    for (int i=1;i<n;i++){
        int a,b; cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
        edge.push_back({a,b});
    }
    for (int i=1;i<=n;i++){
        cin>>is[i];
        all[is[i]].push_back(i);
    }
    dfs(1,1);
    for (int i=1;i<=k;i++){
        for (int j=1;j<all[i].size();j++)
            merge_path(all[i][j-1],all[i][j]);
    }
    for (auto i:edge){
        if (d.find(is[i.x])!=d.find(is[i.y])){
            deg[d.find(is[i.x])]++;
            deg[d.find(is[i.y])]++;
        }
    }
    for (int i=1;i<=k;i++)
        cnt+=(deg[i]==1);
    cout<<(cnt+1)/2<<'\n';
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:83:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int j=1;j<all[i].size();j++)
      |                      ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23860 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23780 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23860 KB Output is correct
12 Correct 13 ms 23748 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23836 KB Output is correct
15 Correct 13 ms 23796 KB Output is correct
16 Correct 13 ms 23852 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 12 ms 23752 KB Output is correct
19 Correct 15 ms 23808 KB Output is correct
20 Correct 15 ms 23840 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23864 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23860 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23780 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23860 KB Output is correct
12 Correct 13 ms 23748 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23836 KB Output is correct
15 Correct 13 ms 23796 KB Output is correct
16 Correct 13 ms 23852 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 12 ms 23752 KB Output is correct
19 Correct 15 ms 23808 KB Output is correct
20 Correct 15 ms 23840 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23864 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 11 ms 23764 KB Output is correct
26 Correct 13 ms 24176 KB Output is correct
27 Correct 13 ms 24020 KB Output is correct
28 Correct 15 ms 24432 KB Output is correct
29 Correct 14 ms 24148 KB Output is correct
30 Correct 15 ms 24140 KB Output is correct
31 Correct 12 ms 23800 KB Output is correct
32 Correct 14 ms 24324 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 14 ms 24156 KB Output is correct
35 Correct 14 ms 24168 KB Output is correct
36 Correct 15 ms 24008 KB Output is correct
37 Correct 14 ms 24100 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 16 ms 24140 KB Output is correct
40 Correct 13 ms 24148 KB Output is correct
41 Correct 17 ms 24160 KB Output is correct
42 Correct 13 ms 24196 KB Output is correct
43 Correct 13 ms 24236 KB Output is correct
44 Correct 12 ms 23764 KB Output is correct
45 Correct 13 ms 24160 KB Output is correct
46 Correct 14 ms 24168 KB Output is correct
47 Correct 12 ms 23764 KB Output is correct
48 Correct 13 ms 24148 KB Output is correct
49 Correct 13 ms 24180 KB Output is correct
50 Correct 15 ms 24188 KB Output is correct
51 Correct 15 ms 24144 KB Output is correct
52 Correct 16 ms 24148 KB Output is correct
53 Correct 14 ms 24168 KB Output is correct
54 Correct 14 ms 24144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23860 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23780 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23860 KB Output is correct
12 Correct 13 ms 23748 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23836 KB Output is correct
15 Correct 13 ms 23796 KB Output is correct
16 Correct 13 ms 23852 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 12 ms 23752 KB Output is correct
19 Correct 15 ms 23808 KB Output is correct
20 Correct 15 ms 23840 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23864 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 11 ms 23764 KB Output is correct
26 Correct 59 ms 32756 KB Output is correct
27 Correct 86 ms 32444 KB Output is correct
28 Correct 15 ms 24020 KB Output is correct
29 Correct 12 ms 23836 KB Output is correct
30 Correct 12 ms 23844 KB Output is correct
31 Correct 70 ms 32376 KB Output is correct
32 Correct 13 ms 24148 KB Output is correct
33 Correct 87 ms 36664 KB Output is correct
34 Correct 98 ms 33932 KB Output is correct
35 Correct 14 ms 24060 KB Output is correct
36 Correct 76 ms 33668 KB Output is correct
37 Correct 14 ms 24056 KB Output is correct
38 Correct 14 ms 24092 KB Output is correct
39 Correct 61 ms 34048 KB Output is correct
40 Correct 13 ms 24276 KB Output is correct
41 Correct 95 ms 34544 KB Output is correct
42 Correct 90 ms 35144 KB Output is correct
43 Correct 12 ms 23764 KB Output is correct
44 Correct 78 ms 38528 KB Output is correct
45 Correct 82 ms 37176 KB Output is correct
46 Correct 14 ms 24148 KB Output is correct
47 Correct 14 ms 24100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 32680 KB Output is correct
2 Correct 73 ms 37072 KB Output is correct
3 Correct 13 ms 24148 KB Output is correct
4 Correct 14 ms 24036 KB Output is correct
5 Correct 12 ms 23812 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 13 ms 24128 KB Output is correct
8 Correct 85 ms 34488 KB Output is correct
9 Correct 16 ms 24068 KB Output is correct
10 Correct 75 ms 32624 KB Output is correct
11 Correct 12 ms 23764 KB Output is correct
12 Correct 73 ms 32080 KB Output is correct
13 Correct 88 ms 34420 KB Output is correct
14 Correct 80 ms 36956 KB Output is correct
15 Correct 59 ms 32636 KB Output is correct
16 Correct 13 ms 24148 KB Output is correct
17 Correct 15 ms 23764 KB Output is correct
18 Correct 68 ms 36592 KB Output is correct
19 Correct 79 ms 38716 KB Output is correct
20 Correct 14 ms 24148 KB Output is correct
21 Correct 12 ms 23760 KB Output is correct
22 Correct 64 ms 34680 KB Output is correct
23 Correct 14 ms 24148 KB Output is correct
24 Correct 76 ms 32924 KB Output is correct
25 Correct 81 ms 38740 KB Output is correct
26 Correct 13 ms 24232 KB Output is correct
27 Correct 13 ms 24228 KB Output is correct
28 Correct 13 ms 24152 KB Output is correct
29 Correct 14 ms 24148 KB Output is correct
30 Correct 13 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23860 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23852 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 14 ms 23780 KB Output is correct
8 Correct 12 ms 23820 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 13 ms 23860 KB Output is correct
12 Correct 13 ms 23748 KB Output is correct
13 Correct 13 ms 23764 KB Output is correct
14 Correct 12 ms 23836 KB Output is correct
15 Correct 13 ms 23796 KB Output is correct
16 Correct 13 ms 23852 KB Output is correct
17 Correct 11 ms 23764 KB Output is correct
18 Correct 12 ms 23752 KB Output is correct
19 Correct 15 ms 23808 KB Output is correct
20 Correct 15 ms 23840 KB Output is correct
21 Correct 12 ms 23764 KB Output is correct
22 Correct 12 ms 23864 KB Output is correct
23 Correct 15 ms 23756 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 11 ms 23764 KB Output is correct
26 Correct 13 ms 24176 KB Output is correct
27 Correct 13 ms 24020 KB Output is correct
28 Correct 15 ms 24432 KB Output is correct
29 Correct 14 ms 24148 KB Output is correct
30 Correct 15 ms 24140 KB Output is correct
31 Correct 12 ms 23800 KB Output is correct
32 Correct 14 ms 24324 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 14 ms 24156 KB Output is correct
35 Correct 14 ms 24168 KB Output is correct
36 Correct 15 ms 24008 KB Output is correct
37 Correct 14 ms 24100 KB Output is correct
38 Correct 12 ms 23764 KB Output is correct
39 Correct 16 ms 24140 KB Output is correct
40 Correct 13 ms 24148 KB Output is correct
41 Correct 17 ms 24160 KB Output is correct
42 Correct 13 ms 24196 KB Output is correct
43 Correct 13 ms 24236 KB Output is correct
44 Correct 12 ms 23764 KB Output is correct
45 Correct 13 ms 24160 KB Output is correct
46 Correct 14 ms 24168 KB Output is correct
47 Correct 12 ms 23764 KB Output is correct
48 Correct 13 ms 24148 KB Output is correct
49 Correct 13 ms 24180 KB Output is correct
50 Correct 15 ms 24188 KB Output is correct
51 Correct 15 ms 24144 KB Output is correct
52 Correct 16 ms 24148 KB Output is correct
53 Correct 14 ms 24168 KB Output is correct
54 Correct 14 ms 24144 KB Output is correct
55 Correct 11 ms 23764 KB Output is correct
56 Correct 59 ms 32756 KB Output is correct
57 Correct 86 ms 32444 KB Output is correct
58 Correct 15 ms 24020 KB Output is correct
59 Correct 12 ms 23836 KB Output is correct
60 Correct 12 ms 23844 KB Output is correct
61 Correct 70 ms 32376 KB Output is correct
62 Correct 13 ms 24148 KB Output is correct
63 Correct 87 ms 36664 KB Output is correct
64 Correct 98 ms 33932 KB Output is correct
65 Correct 14 ms 24060 KB Output is correct
66 Correct 76 ms 33668 KB Output is correct
67 Correct 14 ms 24056 KB Output is correct
68 Correct 14 ms 24092 KB Output is correct
69 Correct 61 ms 34048 KB Output is correct
70 Correct 13 ms 24276 KB Output is correct
71 Correct 95 ms 34544 KB Output is correct
72 Correct 90 ms 35144 KB Output is correct
73 Correct 12 ms 23764 KB Output is correct
74 Correct 78 ms 38528 KB Output is correct
75 Correct 82 ms 37176 KB Output is correct
76 Correct 14 ms 24148 KB Output is correct
77 Correct 14 ms 24100 KB Output is correct
78 Correct 58 ms 32680 KB Output is correct
79 Correct 73 ms 37072 KB Output is correct
80 Correct 13 ms 24148 KB Output is correct
81 Correct 14 ms 24036 KB Output is correct
82 Correct 12 ms 23812 KB Output is correct
83 Correct 12 ms 23764 KB Output is correct
84 Correct 13 ms 24128 KB Output is correct
85 Correct 85 ms 34488 KB Output is correct
86 Correct 16 ms 24068 KB Output is correct
87 Correct 75 ms 32624 KB Output is correct
88 Correct 12 ms 23764 KB Output is correct
89 Correct 73 ms 32080 KB Output is correct
90 Correct 88 ms 34420 KB Output is correct
91 Correct 80 ms 36956 KB Output is correct
92 Correct 59 ms 32636 KB Output is correct
93 Correct 13 ms 24148 KB Output is correct
94 Correct 15 ms 23764 KB Output is correct
95 Correct 68 ms 36592 KB Output is correct
96 Correct 79 ms 38716 KB Output is correct
97 Correct 14 ms 24148 KB Output is correct
98 Correct 12 ms 23760 KB Output is correct
99 Correct 64 ms 34680 KB Output is correct
100 Correct 14 ms 24148 KB Output is correct
101 Correct 76 ms 32924 KB Output is correct
102 Correct 81 ms 38740 KB Output is correct
103 Correct 13 ms 24232 KB Output is correct
104 Correct 13 ms 24228 KB Output is correct
105 Correct 13 ms 24152 KB Output is correct
106 Correct 14 ms 24148 KB Output is correct
107 Correct 13 ms 24148 KB Output is correct
108 Correct 446 ms 86192 KB Output is correct
109 Correct 524 ms 86756 KB Output is correct
110 Correct 527 ms 92536 KB Output is correct
111 Correct 643 ms 111320 KB Output is correct
112 Correct 677 ms 110608 KB Output is correct
113 Correct 515 ms 97656 KB Output is correct
114 Correct 521 ms 74992 KB Output is correct
115 Correct 501 ms 77720 KB Output is correct
116 Correct 682 ms 81404 KB Output is correct
117 Correct 575 ms 99396 KB Output is correct
118 Correct 495 ms 75964 KB Output is correct
119 Correct 584 ms 99396 KB Output is correct
120 Correct 622 ms 109256 KB Output is correct
121 Correct 561 ms 99496 KB Output is correct
122 Correct 689 ms 89076 KB Output is correct
123 Correct 431 ms 100316 KB Output is correct
124 Correct 650 ms 95276 KB Output is correct