답안 #10941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
10941 2014-11-12T11:56:26 Z dohyun0324 우호 조약 체결 (JOI14_friends) C++
100 / 100
112 ms 12444 KB
#include<stdio.h>
#include<vector>
using namespace std;
vector<int>con[200010];
int f,r,n,m,x[200010],y[200010],group[100010],ranking[100010],cnt[100010],arr[100010],w,ch[100010];
long long dap;
int finding(int x)
{
    if(x==group[x]) return x;
    return finding(group[x]);
}
void union_find(int x,int y)
{
    int p,q;
    p=finding(x); q=finding(y);
    if(ranking[p]==ranking[q]){
        group[q]=p;
        ranking[p]++;
    }
    else if(ranking[p]>ranking[q]) group[q]=p;
    else group[p]=q;
}
int main()
{
    int i,j,k;
    scanf("%d %d",&n,&m);
    for(i=1;i<=n;i++) group[i]=i;
    for(i=1;i<=m;i++){
        scanf("%d %d",&x[i],&y[i]);
        con[x[i]].push_back(y[i]);
    }
    for(i=1;i<=n;i++){
        k=con[i].size();
        for(j=0;j<k-1;j++){
            union_find(con[i][j],con[i][j+1]);
            ch[con[i][j]]=1;
        }
        if(j>0) ch[con[i][j]]=1;
    }
    for(i=1;i<=n;i++){
        if(ch[i]) arr[++r]=i;
    }
    while(f!=r){
        f++;
        if(con[arr[f]].size()==0) continue;
        k=con[arr[f]][0];
        union_find(arr[f],k);
        if(ch[k]==0) ch[k]=1, arr[++r]=k;
    }
    for(i=1;i<=n;i++){
        group[i]=finding(i);
        cnt[group[i]]++;
    }
    for(i=1;i<=n;i++) dap+=(long long)cnt[i]*(long long)(cnt[i]-1);
    for(i=1;i<=m;i++){
        if(group[x[i]]!=group[y[i]]) dap++;
    }
    printf("%lld",dap);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 9408 KB Output is correct
2 Correct 0 ms 9408 KB Output is correct
3 Correct 0 ms 9408 KB Output is correct
4 Correct 0 ms 9408 KB Output is correct
5 Correct 0 ms 9408 KB Output is correct
6 Correct 0 ms 9408 KB Output is correct
7 Correct 0 ms 9408 KB Output is correct
8 Correct 4 ms 9408 KB Output is correct
9 Correct 4 ms 9408 KB Output is correct
10 Correct 0 ms 9408 KB Output is correct
11 Correct 0 ms 9408 KB Output is correct
12 Correct 0 ms 9408 KB Output is correct
13 Correct 0 ms 9408 KB Output is correct
14 Correct 0 ms 9408 KB Output is correct
15 Correct 4 ms 9408 KB Output is correct
16 Correct 0 ms 9408 KB Output is correct
17 Correct 4 ms 9408 KB Output is correct
18 Correct 4 ms 9408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9408 KB Output is correct
2 Correct 0 ms 9408 KB Output is correct
3 Correct 8 ms 9540 KB Output is correct
4 Correct 52 ms 10332 KB Output is correct
5 Correct 28 ms 10068 KB Output is correct
6 Correct 76 ms 10728 KB Output is correct
7 Correct 0 ms 9408 KB Output is correct
8 Correct 8 ms 9408 KB Output is correct
9 Correct 12 ms 9540 KB Output is correct
10 Correct 20 ms 9672 KB Output is correct
11 Correct 64 ms 10728 KB Output is correct
12 Correct 12 ms 9540 KB Output is correct
13 Correct 4 ms 9408 KB Output is correct
14 Correct 4 ms 9408 KB Output is correct
15 Correct 4 ms 9408 KB Output is correct
16 Correct 16 ms 9672 KB Output is correct
17 Correct 72 ms 10596 KB Output is correct
18 Correct 4 ms 9540 KB Output is correct
19 Correct 4 ms 9540 KB Output is correct
20 Correct 60 ms 10728 KB Output is correct
21 Correct 0 ms 9408 KB Output is correct
22 Correct 4 ms 9408 KB Output is correct
23 Correct 8 ms 9540 KB Output is correct
24 Correct 4 ms 9408 KB Output is correct
25 Correct 4 ms 9540 KB Output is correct
26 Correct 0 ms 9408 KB Output is correct
27 Correct 4 ms 9540 KB Output is correct
28 Correct 0 ms 9408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 10464 KB Output is correct
2 Correct 96 ms 12180 KB Output is correct
3 Correct 112 ms 12180 KB Output is correct
4 Correct 56 ms 11256 KB Output is correct
5 Correct 16 ms 10596 KB Output is correct
6 Correct 96 ms 11388 KB Output is correct
7 Correct 84 ms 12180 KB Output is correct
8 Correct 84 ms 12180 KB Output is correct
9 Correct 40 ms 11388 KB Output is correct
10 Correct 64 ms 11784 KB Output is correct
11 Correct 100 ms 12180 KB Output is correct
12 Correct 96 ms 12444 KB Output is correct
13 Correct 36 ms 12444 KB Output is correct
14 Correct 68 ms 10860 KB Output is correct
15 Correct 20 ms 10596 KB Output is correct
16 Correct 0 ms 9408 KB Output is correct
17 Correct 0 ms 9408 KB Output is correct
18 Correct 100 ms 11784 KB Output is correct
19 Correct 104 ms 11784 KB Output is correct
20 Correct 48 ms 10992 KB Output is correct
21 Correct 16 ms 10332 KB Output is correct
22 Correct 0 ms 9408 KB Output is correct
23 Correct 32 ms 10068 KB Output is correct
24 Correct 64 ms 10992 KB Output is correct
25 Correct 40 ms 12444 KB Output is correct
26 Correct 48 ms 12444 KB Output is correct
27 Correct 100 ms 12444 KB Output is correct
28 Correct 52 ms 11652 KB Output is correct
29 Correct 48 ms 11652 KB Output is correct
30 Correct 100 ms 12180 KB Output is correct
31 Correct 52 ms 10992 KB Output is correct
32 Correct 48 ms 12444 KB Output is correct
33 Correct 40 ms 12444 KB Output is correct
34 Correct 56 ms 11388 KB Output is correct
35 Correct 44 ms 12444 KB Output is correct
36 Correct 48 ms 12444 KB Output is correct
37 Correct 44 ms 11652 KB Output is correct