답안 #679411

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679411 2023-01-08T08:46:26 Z Ronin13 Mergers (JOI19_mergers) C++14
0 / 100
118 ms 28156 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];
    }
}
int go(int v, int e = -1){
    int ans = 0;
    for(int to : g[v]){
        if(to == e) continue;
        ans += go(to, v);
    }
    if(g[v].size() == 1) return 1;
    return ans;
}

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

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;
   // cout << 1;
    for(int to : g[1]) ansdfs(to, 1);
   cout << max(mx, (sum + 1) / 2);
    return 0;
}

Compilation message

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