답안 #716266

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
716266 2023-03-29T12:25:29 Z Ahmed57 Mergers (JOI19_mergers) C++14
0 / 100
170 ms 90396 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;

const int N = 5e5+5;

int n, k;
vector<int> g[N];
vector<pii> E;
int dep[N], dsu[N], deg[N], par[N];
vector<int> col[N];

int find(int u) { return dsu[u] = dsu[u] == u ? u : find(dsu[u]); }

void init_dep(int u, int p) {
    dep[u] = dep[p] + 1, par[u] = p;
    for(int v : g[u]) if(v != p) init_dep(v, u);
}
int arr[200001] , cnt[200001];
bool mark[200001];
pair<int,map<int,int>> dfs(int i,int pr){
    map<int,int> mp;
    pair<int,map<int,int>> p1;
    int bad = 0;
    mp[arr[i]]++;
    if(cnt[arr[i]]!=1)bad++;
    for(auto j:g[i]){
        if(j!=pr){
            p1 = dfs(j,i);
            bad+=p1.first;
            if(p1.second.size()>mp.size())swap(mp,p1.second);
            for(auto k:p1.second){
                if(mp[k.first]!=0)bad--;
                mp[k.first]+=k.second;
                if(mp[k.first]==cnt[k.first]&&k.second!=cnt[k.first])bad--;
            }
        }
    }
    if(bad==0){
        mark[i] = 1;
    }
    return {bad,mp};
}
int calc(int i,int pr){
    int sum = 0;
    for(auto j:g[i]){
        if(j==pr)continue;
        sum+=calc(j,i);
    }
    if(mark[i]){
        if(sum)return sum;
        else return 1;
    }else return sum;
}
int main() {
    iota(dsu, dsu+N, 0);
    scanf("%d %d", &n, &k);
    for(int i = 1, u, v; i < n; ++i) {
        scanf("%d %d", &u, &v);
        g[u].emplace_back(v), g[v].emplace_back(u);
        E.emplace_back(u, v);
    }
    for(int i = 1, v; i <= n; ++i) {
        scanf("%d", &v);
        arr[i] = v;
        cnt[v]++;
        col[v].emplace_back(i);
    }
    init_dep(1, 0);
    for(int i = 1; i <= k; ++i) if(col[i].size() > 1) {
        for(int j = 1; j < col[i].size(); ++j) {
            int a = find(col[i][j-1]), b = find(col[i][j]);
            while(a != b) {
                if(dep[a] < dep[b]) swap(a, b);
                dsu[a] = find(par[a]);
                a = dsu[a];
            }
        }
    }
    for(auto x : E) {
        int a = find(x.x), b = find(x.y);
        if(a != b) deg[a]++, deg[b]++;
    }
    int cnt = 0;
    for(int i = 1; i <= n; ++i) if(deg[i] == 1) cnt++;
    pair<int,map<int,int>>p1 = dfs(1,0);
    mark[1] = 0;
    int ans = calc(1,0);
    if((ans+1)/2<(cnt+1)/2)assert(0);
    printf("%d\n", (cnt+1) / 2);
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:73:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         for(int j = 1; j < col[i].size(); ++j) {
      |                        ~~^~~~~~~~~~~~~~~
mergers.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         scanf("%d", &v);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25812 KB Output is correct
2 Correct 14 ms 25760 KB Output is correct
3 Correct 13 ms 25788 KB Output is correct
4 Correct 12 ms 25744 KB Output is correct
5 Correct 13 ms 25704 KB Output is correct
6 Correct 12 ms 25692 KB Output is correct
7 Correct 12 ms 25684 KB Output is correct
8 Correct 13 ms 25800 KB Output is correct
9 Correct 12 ms 25684 KB Output is correct
10 Correct 12 ms 25704 KB Output is correct
11 Correct 13 ms 25812 KB Output is correct
12 Correct 13 ms 25788 KB Output is correct
13 Correct 15 ms 25692 KB Output is correct
14 Correct 13 ms 25684 KB Output is correct
15 Correct 13 ms 25684 KB Output is correct
16 Runtime error 38 ms 52024 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25812 KB Output is correct
2 Correct 14 ms 25760 KB Output is correct
3 Correct 13 ms 25788 KB Output is correct
4 Correct 12 ms 25744 KB Output is correct
5 Correct 13 ms 25704 KB Output is correct
6 Correct 12 ms 25692 KB Output is correct
7 Correct 12 ms 25684 KB Output is correct
8 Correct 13 ms 25800 KB Output is correct
9 Correct 12 ms 25684 KB Output is correct
10 Correct 12 ms 25704 KB Output is correct
11 Correct 13 ms 25812 KB Output is correct
12 Correct 13 ms 25788 KB Output is correct
13 Correct 15 ms 25692 KB Output is correct
14 Correct 13 ms 25684 KB Output is correct
15 Correct 13 ms 25684 KB Output is correct
16 Runtime error 38 ms 52024 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25812 KB Output is correct
2 Correct 14 ms 25760 KB Output is correct
3 Correct 13 ms 25788 KB Output is correct
4 Correct 12 ms 25744 KB Output is correct
5 Correct 13 ms 25704 KB Output is correct
6 Correct 12 ms 25692 KB Output is correct
7 Correct 12 ms 25684 KB Output is correct
8 Correct 13 ms 25800 KB Output is correct
9 Correct 12 ms 25684 KB Output is correct
10 Correct 12 ms 25704 KB Output is correct
11 Correct 13 ms 25812 KB Output is correct
12 Correct 13 ms 25788 KB Output is correct
13 Correct 15 ms 25692 KB Output is correct
14 Correct 13 ms 25684 KB Output is correct
15 Correct 13 ms 25684 KB Output is correct
16 Runtime error 38 ms 52024 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 31752 KB Output is correct
2 Runtime error 170 ms 90396 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25812 KB Output is correct
2 Correct 14 ms 25760 KB Output is correct
3 Correct 13 ms 25788 KB Output is correct
4 Correct 12 ms 25744 KB Output is correct
5 Correct 13 ms 25704 KB Output is correct
6 Correct 12 ms 25692 KB Output is correct
7 Correct 12 ms 25684 KB Output is correct
8 Correct 13 ms 25800 KB Output is correct
9 Correct 12 ms 25684 KB Output is correct
10 Correct 12 ms 25704 KB Output is correct
11 Correct 13 ms 25812 KB Output is correct
12 Correct 13 ms 25788 KB Output is correct
13 Correct 15 ms 25692 KB Output is correct
14 Correct 13 ms 25684 KB Output is correct
15 Correct 13 ms 25684 KB Output is correct
16 Runtime error 38 ms 52024 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -