답안 #679247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679247 2023-01-07T22:55:54 Z Ronin13 Mergers (JOI19_mergers) C++14
0 / 100
62 ms 26524 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 2e5 + 1;


int a[nmax][22];
int in[nmax], out[nmax];

int timer = 0;

bool is_ancestor(int x, int y){
    return in[x] <= in[y] && out[y] <= out[x];
}

vector <vector <int> > g(nmax);

vector <int> sz(nmax);
void dfs(int v, int e = -1){
    in[v] = timer++;
    a[v][0] = e;
    sz[v]++;
    for(int to : g[v]){
        if(to == e) continue;
        dfs(to, v);
        sz[v] += sz[to];
    }
    out[v] = timer++;
}

void prep(int n){
    dfs(1);
    for(int j = 1; j <= 20; j++)
        for(int i = 1; i <= n; i++)
        if(a[i][j - 1]) a[i][j] = a[a[i][j - 1]][j - 1];
}

int lca(int u, int v){
    for(int j = 20; j >= 0; j--){
        if(is_ancestor(a[v][j], u))
            continue;
        v = a[v][j];
    }
    return a[v][0];
}

int sum = 0, mx = 0;

vector <int> c(nmax);
vector <int> b(nmax);
vector <int> cnt(nmax);


void DFS(int v, int e = -1){
    for(int to : g[v]){
        if(to == e) continue;
        DFS(to, v);
        b[v] += b[to];
    }
    sz[v] -= b[v];
    if(c[v] == c[1]){
        for(int to : g[v]){
            sum += cnt[to];
            mx = max(mx, cnt[to]);
        }
        return;
    }
    else{
        for(int to : g[v])
            cnt[v] += cnt[to];
    }
    if(!sz[v]){
        cnt[v] = max(cnt[v], 1);
    }

}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    int k; cin >> k;
    vector <vector <int> > vec(k + 1);
    for(int i = 1; i < n; ++i){
        int u, v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1);
    for(int i = 1; i <= n; i++){
        cin >> c[i];
        vec[c[i]].pb(i);
    }
    for(int i = 1; i <= k; i++){
        int l = vec[i][0];
        for(int j = 1; j < vec[i].size(); j++)
            l = lca(l, vec[i][j]);
        b[l] += vec[i].size();
    }
    DFS(1);
    //cout << sum;
    if(mx * 2 >= sum)
        cout << mx << "\n";
    else cout << (sum + 1) / 2 << "\n";
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:102:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for(int j = 1; j < vec[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 8148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 8148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 8148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 21600 KB Output is correct
2 Incorrect 56 ms 26524 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 8148 KB Output isn't correct
2 Halted 0 ms 0 KB -