Submission #386219

# Submission time Handle Problem Language Result Execution time Memory
386219 2021-04-06T06:38:43 Z nafis_shifat Mergers (JOI19_mergers) C++14
0 / 100
111 ms 20580 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);
}
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[n + 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 <= n; i++) {
        if(parent[i] == i && deg[i] == 1) leaf++;
    }
    cout<<(leaf + 1) / 2<<endl;




}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:60:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for(int j = 0; j < con[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~
mergers.cpp:61:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |             for(int k = j + 1; k < con[i].size(); k++) {
      |                                ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'bool dfs(int, int, int)':
mergers.cpp:36:1: warning: control reaches end of non-void function [-Wreturn-type]
   36 | }
      | ^
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 10092 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 10092 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 10092 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 111 ms 20580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 10092 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -