Submission #386223

# Submission time Handle Problem Language Result Execution time Memory
386223 2021-04-06T06:44:45 Z nafis_shifat Mergers (JOI19_mergers) C++14
10 / 100
3000 ms 10980 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
vector<int> v;
vector<int> adj[mxn];
int parent[mxn];
int sz[mxn];
int S[mxn];
vector<int> con[mxn];
int findrep(int u) {
    return parent[u] == u ? u : parent[u] = findrep(parent[u]);
}
void unite(int u,int v) {
    u = findrep(u);
    v = findrep(v);
    if(u == v) return;

    if(sz[u] > sz[v]) swap(u,v);
    parent[u] = v;
    sz[v] += sz[u];
}
bool dfs(int cur,int prev,int src) {
    if(cur == src) return true;


    bool f = false;

    for(int u : adj[cur]) {
        if(u == prev) continue;
        f |= dfs(u,cur,src);
    }
    if(f) v.push_back(cur);
    return f;
}
int main() {
    int n,k;
    cin >> n >> k;

    for(int i = 1; i < n; i++) {
        int u,v;
        cin >> u >> v;

        adj[u].push_back(v);
        adj[v].push_back(u);

    }

    for(int i = 1; i <= n; i++) {
        cin >> S[i];
        con[S[i]].push_back(i);
    }

    for(int i = 1; i <= k; i++) {
        parent[i] = i;
        sz[i] = 1;
    }
    for(int i = 1; i <= k; i++) {
        for(int j = 0; j < con[i].size(); j++) {
            for(int k = j + 1; k < con[i].size(); k++) {
                v.clear();
                dfs(con[i][j],-1,con[i][k]);

                for(int x : v) {
                    unite(i,S[x]);
                }
            }
        }
    }
    int deg[k + 1] = {};
    for(int i = 1; i <= n; i++) {
        int x = findrep(S[i]);
        for(int u : adj[i]) {
            if(x == findrep(S[u])) continue;
            deg[findrep(S[u])]++;
        }
    }

    int leaf = 0;
    for(int i = 1; i <= k; i++) {
        if(parent[i] == i && deg[i] == 1) leaf++;
    }
    cout<<(leaf + 1) / 2<<endl;




}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:61:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for(int j = 0; j < con[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~
mergers.cpp:62:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |             for(int k = j + 1; k < con[i].size(); k++) {
      |                                ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 5 ms 4972 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 8 ms 4972 KB Output is correct
7 Correct 4 ms 4972 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 5 ms 4972 KB Output is correct
12 Correct 5 ms 4972 KB Output is correct
13 Correct 4 ms 4972 KB Output is correct
14 Correct 5 ms 4972 KB Output is correct
15 Correct 5 ms 4972 KB Output is correct
16 Correct 4 ms 4972 KB Output is correct
17 Correct 4 ms 4972 KB Output is correct
18 Correct 5 ms 4972 KB Output is correct
19 Correct 4 ms 4972 KB Output is correct
20 Correct 4 ms 4972 KB Output is correct
21 Correct 5 ms 4972 KB Output is correct
22 Correct 5 ms 4992 KB Output is correct
23 Correct 4 ms 4972 KB Output is correct
24 Correct 5 ms 4972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 5 ms 4972 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 8 ms 4972 KB Output is correct
7 Correct 4 ms 4972 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 5 ms 4972 KB Output is correct
12 Correct 5 ms 4972 KB Output is correct
13 Correct 4 ms 4972 KB Output is correct
14 Correct 5 ms 4972 KB Output is correct
15 Correct 5 ms 4972 KB Output is correct
16 Correct 4 ms 4972 KB Output is correct
17 Correct 4 ms 4972 KB Output is correct
18 Correct 5 ms 4972 KB Output is correct
19 Correct 4 ms 4972 KB Output is correct
20 Correct 4 ms 4972 KB Output is correct
21 Correct 5 ms 4972 KB Output is correct
22 Correct 5 ms 4992 KB Output is correct
23 Correct 4 ms 4972 KB Output is correct
24 Correct 5 ms 4972 KB Output is correct
25 Correct 4 ms 4972 KB Output is correct
26 Correct 33 ms 5228 KB Output is correct
27 Execution timed out 3067 ms 5228 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 5 ms 4972 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 8 ms 4972 KB Output is correct
7 Correct 4 ms 4972 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 5 ms 4972 KB Output is correct
12 Correct 5 ms 4972 KB Output is correct
13 Correct 4 ms 4972 KB Output is correct
14 Correct 5 ms 4972 KB Output is correct
15 Correct 5 ms 4972 KB Output is correct
16 Correct 4 ms 4972 KB Output is correct
17 Correct 4 ms 4972 KB Output is correct
18 Correct 5 ms 4972 KB Output is correct
19 Correct 4 ms 4972 KB Output is correct
20 Correct 4 ms 4972 KB Output is correct
21 Correct 5 ms 4972 KB Output is correct
22 Correct 5 ms 4992 KB Output is correct
23 Correct 4 ms 4972 KB Output is correct
24 Correct 5 ms 4972 KB Output is correct
25 Correct 5 ms 4972 KB Output is correct
26 Execution timed out 3078 ms 10980 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3054 ms 10340 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB Output is correct
2 Correct 5 ms 4972 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 4972 KB Output is correct
5 Correct 4 ms 4972 KB Output is correct
6 Correct 8 ms 4972 KB Output is correct
7 Correct 4 ms 4972 KB Output is correct
8 Correct 5 ms 4972 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4972 KB Output is correct
11 Correct 5 ms 4972 KB Output is correct
12 Correct 5 ms 4972 KB Output is correct
13 Correct 4 ms 4972 KB Output is correct
14 Correct 5 ms 4972 KB Output is correct
15 Correct 5 ms 4972 KB Output is correct
16 Correct 4 ms 4972 KB Output is correct
17 Correct 4 ms 4972 KB Output is correct
18 Correct 5 ms 4972 KB Output is correct
19 Correct 4 ms 4972 KB Output is correct
20 Correct 4 ms 4972 KB Output is correct
21 Correct 5 ms 4972 KB Output is correct
22 Correct 5 ms 4992 KB Output is correct
23 Correct 4 ms 4972 KB Output is correct
24 Correct 5 ms 4972 KB Output is correct
25 Correct 4 ms 4972 KB Output is correct
26 Correct 33 ms 5228 KB Output is correct
27 Execution timed out 3067 ms 5228 KB Time limit exceeded
28 Halted 0 ms 0 KB -