답안 #1049412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1049412 2024-08-08T18:09:52 Z Jarif_Rahman Mergers (JOI19_mergers) C++17
0 / 100
3000 ms 9272 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

int n, k;
vector<vector<int>> tree;
vector<int> sz;
vector<int> state, scnt;
vector<bool> sep;

void add(int nd, int ss, vector<int> &cnt){
    for(int x: tree[nd]) if(x != ss) add(x, nd, cnt);
    cnt[state[nd]]++;
}
void dfs(int nd, int ss){
    for(int x: tree[nd]) if(x != ss) dfs(x, nd);
    if(ss == -1) return;
    vector<int> cnt(k, 0);
    add(nd, ss, cnt);
    for(int i = 0; i < k; i++) sep[nd] = sep[nd]&(cnt[i] == 0 || cnt[i] == scnt[i]);
}

int ans = 0;
int dfs2(int nd, int ss){
    int total = 0;
    for(int x: tree[nd]) if(x != ss) total+=dfs2(x, nd);
    if(!sep[nd]) return total;
    ans+=(total+1)/2;
    return 1;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    tree.resize(n);
    state.resize(n);
    scnt.resize(k, 0);
    sep.resize(n, 1);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    for(int i = 0; i < n; i++){
        cin >> state[i]; state[i]--;
        scnt[state[i]]++;
    }

    dfs(0, -1);
    dfs2(0, -1);
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 8152 KB Output is correct
2 Execution timed out 3091 ms 9272 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -