답안 #679661

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679661 2023-01-08T20:27:50 Z keta_tsimakuridze Mergers (JOI19_mergers) C++14
10 / 100
130 ms 35480 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, p[N], P[N][20], X[N], in[N], out[N];
int ans = 0, timer = 0;
vector<int> V[N], x[N];
bool cmp(int u, int v) {
    return (in[u] < in[v]);
}
int find(int x) {
    return (p[x] == x ? x : p[x] = find(p[x]));
}

void dfs0(int u) {
    in[u] = ++timer;
    for(int i = 1; i <= 18; i++) P[u][i] = P[P[u][i - 1]][i - 1];
    for(int i = 0; i < V[u].size(); i++) {
        if(V[u][i] == P[u][0]) continue;
        P[V[u][i]][0] = u;
        dfs0(V[u][i]);
    }
    out[u] = timer;
}
bool check(int u, int v) {
    return (in[u] <= in[v] && out[v] <= out[u]);
}
int lca(int u, int v) {
    if(check(u, v)) return u;
    if(check(v, u)) return v;

    for(int i = 18; i >= 0; i--) {
        if(P[u][i] && !check(P[u][i], v)) u = P[u][i];
    }
    return P[u][0];
}
void up(int u, int v, int lca) {
    int cur = u;
    while(true) {
        cur = find(cur);
        if(cur == 1 || (P[cur][0] != lca && check(P[cur][0], v))) break;
        p[cur] = find(P[cur][0]);
        cur = p[cur];
    }
}
void dfs(int u) {

    for(int i = 0; i < V[u].size(); i++) {
        dfs(V[u][i]);
        X[u] += X[V[u][i]];
    }
    ans += X[u] / 2;
    if(u - 1) {
        if(!X[u]) X[u] = 1;

    } X[u] %= 2;
}
main(){
    int n, m;
    cin >> n >> m;
    for(int i =1; i <= n; i++) p[i] = i;
    for(int i = 2; i <= n; i++) {
        int u, v;
        cin >> u >> v;
        V[u].push_back(v);
        V[v].push_back(u);
    }
    dfs0(1);
    for(int i = 1; i <= n; i++) {
        int a; cin >> a;
        x[a].push_back(i);
    }
    for(int i = 1; i <= m; i++) {

        sort(x[i].begin(), x[i].end(), cmp);

        for(int j = 1; j < x[i].size(); j++) {

            int u = x[i][j - 1], v = x[i][j];
            int l = lca(u, v);
            up(u, v, l);
            up(v, u, l);
        }
    }
    for(int i = 1; i <= n; i++) V[i].clear();
    for(int i = 2; i <= n; i++) if(find(i) != find(P[i][0])) V[find(P[i][0])].push_back(find(i));
    dfs(1);

    ans += X[1];
    cout << ans;

 }

Compilation message

mergers.cpp: In function 'void dfs0(long long int)':
mergers.cpp:21:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int i = 0; i < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
mergers.cpp: In function 'void dfs(long long int)':
mergers.cpp:51:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i = 0; i < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
mergers.cpp: At global scope:
mergers.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   61 | main(){
      | ^~~~
mergers.cpp: In function 'int main()':
mergers.cpp:80:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |         for(int j = 1; j < x[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9688 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9648 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9624 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9724 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9688 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9648 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9624 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9724 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Incorrect 5 ms 9684 KB Output isn't correct
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9688 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9648 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9624 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9724 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Incorrect 4 ms 9684 KB Output isn't correct
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 130 ms 32676 KB Output is correct
2 Incorrect 128 ms 35480 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9688 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9648 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9624 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9676 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 5 ms 9724 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Incorrect 5 ms 9684 KB Output isn't correct
26 Halted 0 ms 0 KB -