Submission #927608

# Submission time Handle Problem Language Result Execution time Memory
927608 2024-02-15T07:10:07 Z guagua0407 Mergers (JOI19_mergers) C++17
100 / 100
777 ms 138756 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=5e5+5;
vector<int> adj[mxn];
int c[mxn];
bool ok[mxn];
map<int,int> mp[mxn];
int cnt[mxn];
int sum[mxn];
int deg[mxn];
int sumok=0;
int ans=0;

void dfs(int v,int p=0){
    mp[v][c[v]]++;
    if(mp[v][c[v]]==cnt[c[v]]) mp[v].erase(c[v]);
    for(auto u:adj[v]){
        if(u==p) continue;
        dfs(u,v);
        if(mp[v].size()<mp[u].size()) swap(mp[v],mp[u]);
        for(auto x:mp[u]){
            mp[v][x.f]+=x.s;
            if(mp[v][x.f]==cnt[x.f]) mp[v].erase(x.f);
        }
        mp[u].clear();
    }
    if(mp[v].empty() and v!=1){
        ok[v]=true;
        ok[p]=true;
    }
}

void dfs2(int v,int p=0){
    if(ok[v]) sum[v]++;
    for(auto u:adj[v]){
        if(u==p) continue;
        dfs2(u,v);
        if(sum[u]>0) deg[v]++;
        sum[v]+=sum[u];
    }
    if(sum[v]!=sumok){
        deg[v]++;
    }
    if(ok[v] and deg[v]<2) ans++;
}

int main() {_
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        cin>>c[i];
        cnt[c[i]]++;
    }
    dfs(1);
    for(int i=1;i<=n;i++){
        sumok+=ok[i];
    }
    dfs2(1);
    cout<<(ans+1)/2<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

mergers.cpp: In function 'void setIO(std::string)':
mergers.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 9 ms 41560 KB Output is correct
3 Correct 9 ms 41792 KB Output is correct
4 Correct 9 ms 43832 KB Output is correct
5 Correct 9 ms 41560 KB Output is correct
6 Correct 8 ms 41816 KB Output is correct
7 Correct 10 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 10 ms 43612 KB Output is correct
10 Correct 8 ms 43612 KB Output is correct
11 Correct 9 ms 43772 KB Output is correct
12 Correct 10 ms 41560 KB Output is correct
13 Correct 9 ms 43840 KB Output is correct
14 Correct 8 ms 43612 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 11 ms 43720 KB Output is correct
17 Correct 9 ms 43612 KB Output is correct
18 Correct 10 ms 41784 KB Output is correct
19 Correct 9 ms 43608 KB Output is correct
20 Correct 9 ms 43740 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41564 KB Output is correct
24 Correct 9 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 9 ms 41560 KB Output is correct
3 Correct 9 ms 41792 KB Output is correct
4 Correct 9 ms 43832 KB Output is correct
5 Correct 9 ms 41560 KB Output is correct
6 Correct 8 ms 41816 KB Output is correct
7 Correct 10 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 10 ms 43612 KB Output is correct
10 Correct 8 ms 43612 KB Output is correct
11 Correct 9 ms 43772 KB Output is correct
12 Correct 10 ms 41560 KB Output is correct
13 Correct 9 ms 43840 KB Output is correct
14 Correct 8 ms 43612 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 11 ms 43720 KB Output is correct
17 Correct 9 ms 43612 KB Output is correct
18 Correct 10 ms 41784 KB Output is correct
19 Correct 9 ms 43608 KB Output is correct
20 Correct 9 ms 43740 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41564 KB Output is correct
24 Correct 9 ms 43612 KB Output is correct
25 Correct 10 ms 43864 KB Output is correct
26 Correct 11 ms 43840 KB Output is correct
27 Correct 10 ms 43868 KB Output is correct
28 Correct 10 ms 44120 KB Output is correct
29 Correct 11 ms 43840 KB Output is correct
30 Correct 13 ms 41820 KB Output is correct
31 Correct 10 ms 43612 KB Output is correct
32 Correct 10 ms 44124 KB Output is correct
33 Correct 8 ms 43612 KB Output is correct
34 Correct 9 ms 41820 KB Output is correct
35 Correct 10 ms 43972 KB Output is correct
36 Correct 10 ms 41816 KB Output is correct
37 Correct 10 ms 41820 KB Output is correct
38 Correct 9 ms 43612 KB Output is correct
39 Correct 11 ms 41820 KB Output is correct
40 Correct 10 ms 41820 KB Output is correct
41 Correct 9 ms 41820 KB Output is correct
42 Correct 11 ms 43868 KB Output is correct
43 Correct 10 ms 42484 KB Output is correct
44 Correct 8 ms 43612 KB Output is correct
45 Correct 10 ms 44044 KB Output is correct
46 Correct 10 ms 43868 KB Output is correct
47 Correct 8 ms 43612 KB Output is correct
48 Correct 10 ms 43864 KB Output is correct
49 Correct 10 ms 43864 KB Output is correct
50 Correct 10 ms 43868 KB Output is correct
51 Correct 12 ms 43868 KB Output is correct
52 Correct 10 ms 41820 KB Output is correct
53 Correct 10 ms 43864 KB Output is correct
54 Correct 11 ms 42076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 9 ms 41560 KB Output is correct
3 Correct 9 ms 41792 KB Output is correct
4 Correct 9 ms 43832 KB Output is correct
5 Correct 9 ms 41560 KB Output is correct
6 Correct 8 ms 41816 KB Output is correct
7 Correct 10 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 10 ms 43612 KB Output is correct
10 Correct 8 ms 43612 KB Output is correct
11 Correct 9 ms 43772 KB Output is correct
12 Correct 10 ms 41560 KB Output is correct
13 Correct 9 ms 43840 KB Output is correct
14 Correct 8 ms 43612 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 11 ms 43720 KB Output is correct
17 Correct 9 ms 43612 KB Output is correct
18 Correct 10 ms 41784 KB Output is correct
19 Correct 9 ms 43608 KB Output is correct
20 Correct 9 ms 43740 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41564 KB Output is correct
24 Correct 9 ms 43612 KB Output is correct
25 Correct 9 ms 43612 KB Output is correct
26 Correct 48 ms 46544 KB Output is correct
27 Correct 68 ms 48492 KB Output is correct
28 Correct 10 ms 43864 KB Output is correct
29 Correct 10 ms 43612 KB Output is correct
30 Correct 10 ms 43716 KB Output is correct
31 Correct 54 ms 48468 KB Output is correct
32 Correct 9 ms 41820 KB Output is correct
33 Correct 87 ms 66104 KB Output is correct
34 Correct 64 ms 48472 KB Output is correct
35 Correct 10 ms 41820 KB Output is correct
36 Correct 51 ms 50512 KB Output is correct
37 Correct 10 ms 41820 KB Output is correct
38 Correct 9 ms 41820 KB Output is correct
39 Correct 45 ms 46704 KB Output is correct
40 Correct 10 ms 42484 KB Output is correct
41 Correct 50 ms 46576 KB Output is correct
42 Correct 76 ms 52820 KB Output is correct
43 Correct 9 ms 43612 KB Output is correct
44 Correct 80 ms 64444 KB Output is correct
45 Correct 80 ms 58448 KB Output is correct
46 Correct 10 ms 41820 KB Output is correct
47 Correct 11 ms 41920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 45264 KB Output is correct
2 Correct 48 ms 47340 KB Output is correct
3 Correct 10 ms 43868 KB Output is correct
4 Correct 9 ms 43784 KB Output is correct
5 Correct 8 ms 43612 KB Output is correct
6 Correct 8 ms 43612 KB Output is correct
7 Correct 9 ms 41740 KB Output is correct
8 Correct 80 ms 47224 KB Output is correct
9 Correct 11 ms 41820 KB Output is correct
10 Correct 66 ms 47092 KB Output is correct
11 Correct 9 ms 43612 KB Output is correct
12 Correct 53 ms 48548 KB Output is correct
13 Correct 57 ms 48732 KB Output is correct
14 Correct 55 ms 48720 KB Output is correct
15 Correct 48 ms 46676 KB Output is correct
16 Correct 12 ms 43840 KB Output is correct
17 Correct 10 ms 43864 KB Output is correct
18 Correct 55 ms 49484 KB Output is correct
19 Correct 71 ms 56088 KB Output is correct
20 Correct 10 ms 43868 KB Output is correct
21 Correct 9 ms 44080 KB Output is correct
22 Correct 70 ms 49956 KB Output is correct
23 Correct 15 ms 43868 KB Output is correct
24 Correct 51 ms 47108 KB Output is correct
25 Correct 60 ms 53440 KB Output is correct
26 Correct 9 ms 43864 KB Output is correct
27 Correct 9 ms 43868 KB Output is correct
28 Correct 10 ms 43868 KB Output is correct
29 Correct 9 ms 41820 KB Output is correct
30 Correct 10 ms 43868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 43608 KB Output is correct
2 Correct 9 ms 41560 KB Output is correct
3 Correct 9 ms 41792 KB Output is correct
4 Correct 9 ms 43832 KB Output is correct
5 Correct 9 ms 41560 KB Output is correct
6 Correct 8 ms 41816 KB Output is correct
7 Correct 10 ms 41564 KB Output is correct
8 Correct 10 ms 41564 KB Output is correct
9 Correct 10 ms 43612 KB Output is correct
10 Correct 8 ms 43612 KB Output is correct
11 Correct 9 ms 43772 KB Output is correct
12 Correct 10 ms 41560 KB Output is correct
13 Correct 9 ms 43840 KB Output is correct
14 Correct 8 ms 43612 KB Output is correct
15 Correct 8 ms 41564 KB Output is correct
16 Correct 11 ms 43720 KB Output is correct
17 Correct 9 ms 43612 KB Output is correct
18 Correct 10 ms 41784 KB Output is correct
19 Correct 9 ms 43608 KB Output is correct
20 Correct 9 ms 43740 KB Output is correct
21 Correct 8 ms 41564 KB Output is correct
22 Correct 8 ms 41564 KB Output is correct
23 Correct 9 ms 41564 KB Output is correct
24 Correct 9 ms 43612 KB Output is correct
25 Correct 10 ms 43864 KB Output is correct
26 Correct 11 ms 43840 KB Output is correct
27 Correct 10 ms 43868 KB Output is correct
28 Correct 10 ms 44120 KB Output is correct
29 Correct 11 ms 43840 KB Output is correct
30 Correct 13 ms 41820 KB Output is correct
31 Correct 10 ms 43612 KB Output is correct
32 Correct 10 ms 44124 KB Output is correct
33 Correct 8 ms 43612 KB Output is correct
34 Correct 9 ms 41820 KB Output is correct
35 Correct 10 ms 43972 KB Output is correct
36 Correct 10 ms 41816 KB Output is correct
37 Correct 10 ms 41820 KB Output is correct
38 Correct 9 ms 43612 KB Output is correct
39 Correct 11 ms 41820 KB Output is correct
40 Correct 10 ms 41820 KB Output is correct
41 Correct 9 ms 41820 KB Output is correct
42 Correct 11 ms 43868 KB Output is correct
43 Correct 10 ms 42484 KB Output is correct
44 Correct 8 ms 43612 KB Output is correct
45 Correct 10 ms 44044 KB Output is correct
46 Correct 10 ms 43868 KB Output is correct
47 Correct 8 ms 43612 KB Output is correct
48 Correct 10 ms 43864 KB Output is correct
49 Correct 10 ms 43864 KB Output is correct
50 Correct 10 ms 43868 KB Output is correct
51 Correct 12 ms 43868 KB Output is correct
52 Correct 10 ms 41820 KB Output is correct
53 Correct 10 ms 43864 KB Output is correct
54 Correct 11 ms 42076 KB Output is correct
55 Correct 9 ms 43612 KB Output is correct
56 Correct 48 ms 46544 KB Output is correct
57 Correct 68 ms 48492 KB Output is correct
58 Correct 10 ms 43864 KB Output is correct
59 Correct 10 ms 43612 KB Output is correct
60 Correct 10 ms 43716 KB Output is correct
61 Correct 54 ms 48468 KB Output is correct
62 Correct 9 ms 41820 KB Output is correct
63 Correct 87 ms 66104 KB Output is correct
64 Correct 64 ms 48472 KB Output is correct
65 Correct 10 ms 41820 KB Output is correct
66 Correct 51 ms 50512 KB Output is correct
67 Correct 10 ms 41820 KB Output is correct
68 Correct 9 ms 41820 KB Output is correct
69 Correct 45 ms 46704 KB Output is correct
70 Correct 10 ms 42484 KB Output is correct
71 Correct 50 ms 46576 KB Output is correct
72 Correct 76 ms 52820 KB Output is correct
73 Correct 9 ms 43612 KB Output is correct
74 Correct 80 ms 64444 KB Output is correct
75 Correct 80 ms 58448 KB Output is correct
76 Correct 10 ms 41820 KB Output is correct
77 Correct 11 ms 41920 KB Output is correct
78 Correct 53 ms 45264 KB Output is correct
79 Correct 48 ms 47340 KB Output is correct
80 Correct 10 ms 43868 KB Output is correct
81 Correct 9 ms 43784 KB Output is correct
82 Correct 8 ms 43612 KB Output is correct
83 Correct 8 ms 43612 KB Output is correct
84 Correct 9 ms 41740 KB Output is correct
85 Correct 80 ms 47224 KB Output is correct
86 Correct 11 ms 41820 KB Output is correct
87 Correct 66 ms 47092 KB Output is correct
88 Correct 9 ms 43612 KB Output is correct
89 Correct 53 ms 48548 KB Output is correct
90 Correct 57 ms 48732 KB Output is correct
91 Correct 55 ms 48720 KB Output is correct
92 Correct 48 ms 46676 KB Output is correct
93 Correct 12 ms 43840 KB Output is correct
94 Correct 10 ms 43864 KB Output is correct
95 Correct 55 ms 49484 KB Output is correct
96 Correct 71 ms 56088 KB Output is correct
97 Correct 10 ms 43868 KB Output is correct
98 Correct 9 ms 44080 KB Output is correct
99 Correct 70 ms 49956 KB Output is correct
100 Correct 15 ms 43868 KB Output is correct
101 Correct 51 ms 47108 KB Output is correct
102 Correct 60 ms 53440 KB Output is correct
103 Correct 9 ms 43864 KB Output is correct
104 Correct 9 ms 43868 KB Output is correct
105 Correct 10 ms 43868 KB Output is correct
106 Correct 9 ms 41820 KB Output is correct
107 Correct 10 ms 43868 KB Output is correct
108 Correct 456 ms 76444 KB Output is correct
109 Correct 615 ms 111740 KB Output is correct
110 Correct 633 ms 138756 KB Output is correct
111 Correct 631 ms 112060 KB Output is correct
112 Correct 570 ms 94296 KB Output is correct
113 Correct 360 ms 72760 KB Output is correct
114 Correct 449 ms 67804 KB Output is correct
115 Correct 427 ms 67408 KB Output is correct
116 Correct 670 ms 70644 KB Output is correct
117 Correct 502 ms 70136 KB Output is correct
118 Correct 438 ms 68232 KB Output is correct
119 Correct 494 ms 70020 KB Output is correct
120 Correct 576 ms 95684 KB Output is correct
121 Correct 500 ms 69976 KB Output is correct
122 Correct 777 ms 78636 KB Output is correct
123 Correct 284 ms 71500 KB Output is correct
124 Correct 538 ms 69632 KB Output is correct