답안 #236410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236410 2020-06-02T04:53:16 Z Charis02 Mergers (JOI19_mergers) C++14
48 / 100
3000 ms 29924 KB
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define mid (low+high)/2
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 300004
#define INF 1e9+7

using namespace std;

struct dsu{
    ll par[N];
    ll sz[N];
    ll col[N];

    ll get(ll x)
    {
        return (par[x] == x) ? x : get(par[x]);
    }

    void join(ll x,ll y)
    {
        ll fx=get(x);
        ll fy = get(y);
        if(fx==fy)
            return;
        if(sz[fx]<sz[fy])
            swap(fx,fy);
        sz[fx]+=sz[fy];
        par[fy]=fx;
    }

    ll get_col(ll x)
    {
        return col[get(x)];
    }
};

ll n,k,state[N],answer;
ll ar[N];
vector < ll > graph[N];
ll cnt = 1;
dsu comp;
vector < ll > fin[N];
ll deg[N];

bool check(ll cur,ll par,ll col)
{
    bool res = false;
    if(state[cur] == col)
        res = true;

    rep(i,0,graph[cur].size())
    {
        ll v =graph[cur][i];

        if(v==par)
            continue;

        if(check(v,cur,col))
        {
            comp.join(cur,v);
            res=true;
        }
    }

    return res;
}

void process_paths()
{
    rep(i,1,n+1)
    {
        if(ar[state[i]] != 0)
            continue;

        ar[state[i]] = cnt++;
        check(i,i,state[i]);
    }

    rep(i,1,n+1)
    {
        if(comp.get_col(i) == 0)
            comp.col[comp.get(i)] = cnt++;
        ar[i] = comp.get_col(i);
    }

    rep(i,1,n+1)
    {
        rep(j,0,graph[i].size())
        {
            ll v = graph[i][j];
            if(ar[i] != ar[v])
                deg[ar[i]]++;
        }
    }

    ll leaves = 0;

    rep(i,1,cnt)
    {
        if(deg[i] == 1)
            leaves++;
    }

    answer = (leaves+1)/2;

    return;
}

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

    cin >> n >> k;

    rep(i,0,n-1)
    {
        ll a,b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    rep(i,1,n+1)
    {
        comp.par[i]=i;
        comp.sz[i]=1;
        cin >> state[i];
    }

    process_paths();

    cout << answer << endl;

    return 0;
}

Compilation message

mergers.cpp: In function 'bool check(long long int, long long int, long long int)':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:62:9:
     rep(i,0,graph[cur].size())
         ~~~~~~~~~~~~~~~~~~~~~       
mergers.cpp:62:5: note: in expansion of macro 'rep'
     rep(i,0,graph[cur].size())
     ^~~
mergers.cpp: In function 'void process_paths()':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:99:13:
         rep(j,0,graph[i].size())
             ~~~~~~~~~~~~~~~~~~~     
mergers.cpp:99:9: note: in expansion of macro 'rep'
         rep(j,0,graph[i].size())
         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 11 ms 14464 KB Output is correct
13 Correct 11 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 11 ms 14464 KB Output is correct
18 Correct 11 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 11 ms 14464 KB Output is correct
13 Correct 11 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 11 ms 14464 KB Output is correct
18 Correct 11 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 126 ms 14712 KB Output is correct
27 Correct 16 ms 14720 KB Output is correct
28 Correct 177 ms 14848 KB Output is correct
29 Correct 161 ms 14720 KB Output is correct
30 Correct 23 ms 14720 KB Output is correct
31 Correct 12 ms 14592 KB Output is correct
32 Correct 220 ms 14936 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 14 ms 14720 KB Output is correct
35 Correct 158 ms 14724 KB Output is correct
36 Correct 15 ms 14720 KB Output is correct
37 Correct 88 ms 14712 KB Output is correct
38 Correct 12 ms 14464 KB Output is correct
39 Correct 90 ms 14712 KB Output is correct
40 Correct 13 ms 14720 KB Output is correct
41 Correct 13 ms 14720 KB Output is correct
42 Correct 41 ms 14720 KB Output is correct
43 Correct 13 ms 14848 KB Output is correct
44 Correct 12 ms 14464 KB Output is correct
45 Correct 207 ms 14720 KB Output is correct
46 Correct 87 ms 14840 KB Output is correct
47 Correct 11 ms 14464 KB Output is correct
48 Correct 45 ms 14712 KB Output is correct
49 Correct 70 ms 14720 KB Output is correct
50 Correct 175 ms 14968 KB Output is correct
51 Correct 49 ms 14712 KB Output is correct
52 Correct 14 ms 14720 KB Output is correct
53 Correct 43 ms 14720 KB Output is correct
54 Correct 16 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 11 ms 14464 KB Output is correct
13 Correct 11 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 11 ms 14464 KB Output is correct
18 Correct 11 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 134 ms 21616 KB Output is correct
27 Correct 366 ms 22904 KB Output is correct
28 Correct 16 ms 14720 KB Output is correct
29 Correct 12 ms 14464 KB Output is correct
30 Correct 12 ms 14464 KB Output is correct
31 Correct 480 ms 22904 KB Output is correct
32 Correct 14 ms 14720 KB Output is correct
33 Correct 787 ms 29924 KB Output is correct
34 Correct 428 ms 22904 KB Output is correct
35 Correct 16 ms 14720 KB Output is correct
36 Correct 584 ms 23288 KB Output is correct
37 Correct 15 ms 14720 KB Output is correct
38 Correct 13 ms 14720 KB Output is correct
39 Correct 154 ms 22964 KB Output is correct
40 Correct 13 ms 14976 KB Output is correct
41 Correct 77 ms 22648 KB Output is correct
42 Correct 712 ms 27000 KB Output is correct
43 Correct 12 ms 14464 KB Output is correct
44 Correct 95 ms 29688 KB Output is correct
45 Correct 612 ms 27280 KB Output is correct
46 Correct 14 ms 14720 KB Output is correct
47 Correct 16 ms 14720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 21496 KB Output is correct
2 Execution timed out 3084 ms 23400 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 11 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 11 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 12 ms 14464 KB Output is correct
12 Correct 11 ms 14464 KB Output is correct
13 Correct 11 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 12 ms 14464 KB Output is correct
17 Correct 11 ms 14464 KB Output is correct
18 Correct 11 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 11 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 126 ms 14712 KB Output is correct
27 Correct 16 ms 14720 KB Output is correct
28 Correct 177 ms 14848 KB Output is correct
29 Correct 161 ms 14720 KB Output is correct
30 Correct 23 ms 14720 KB Output is correct
31 Correct 12 ms 14592 KB Output is correct
32 Correct 220 ms 14936 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 14 ms 14720 KB Output is correct
35 Correct 158 ms 14724 KB Output is correct
36 Correct 15 ms 14720 KB Output is correct
37 Correct 88 ms 14712 KB Output is correct
38 Correct 12 ms 14464 KB Output is correct
39 Correct 90 ms 14712 KB Output is correct
40 Correct 13 ms 14720 KB Output is correct
41 Correct 13 ms 14720 KB Output is correct
42 Correct 41 ms 14720 KB Output is correct
43 Correct 13 ms 14848 KB Output is correct
44 Correct 12 ms 14464 KB Output is correct
45 Correct 207 ms 14720 KB Output is correct
46 Correct 87 ms 14840 KB Output is correct
47 Correct 11 ms 14464 KB Output is correct
48 Correct 45 ms 14712 KB Output is correct
49 Correct 70 ms 14720 KB Output is correct
50 Correct 175 ms 14968 KB Output is correct
51 Correct 49 ms 14712 KB Output is correct
52 Correct 14 ms 14720 KB Output is correct
53 Correct 43 ms 14720 KB Output is correct
54 Correct 16 ms 14720 KB Output is correct
55 Correct 12 ms 14464 KB Output is correct
56 Correct 134 ms 21616 KB Output is correct
57 Correct 366 ms 22904 KB Output is correct
58 Correct 16 ms 14720 KB Output is correct
59 Correct 12 ms 14464 KB Output is correct
60 Correct 12 ms 14464 KB Output is correct
61 Correct 480 ms 22904 KB Output is correct
62 Correct 14 ms 14720 KB Output is correct
63 Correct 787 ms 29924 KB Output is correct
64 Correct 428 ms 22904 KB Output is correct
65 Correct 16 ms 14720 KB Output is correct
66 Correct 584 ms 23288 KB Output is correct
67 Correct 15 ms 14720 KB Output is correct
68 Correct 13 ms 14720 KB Output is correct
69 Correct 154 ms 22964 KB Output is correct
70 Correct 13 ms 14976 KB Output is correct
71 Correct 77 ms 22648 KB Output is correct
72 Correct 712 ms 27000 KB Output is correct
73 Correct 12 ms 14464 KB Output is correct
74 Correct 95 ms 29688 KB Output is correct
75 Correct 612 ms 27280 KB Output is correct
76 Correct 14 ms 14720 KB Output is correct
77 Correct 16 ms 14720 KB Output is correct
78 Correct 135 ms 21496 KB Output is correct
79 Execution timed out 3084 ms 23400 KB Time limit exceeded
80 Halted 0 ms 0 KB -