답안 #6920

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
6920 2014-07-10T13:21:10 Z tncks0121 우호 조약 체결 (JOI14_friends) C++
100 / 100
116 ms 12424 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>

using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

#define pb push_back

const int N_ = 100005;

int N, M;
vector<int> Gph[N_];
bool visited[N_];

namespace DS {
    int group[N_];
    
    void init () {
        for(int i = 1; i <= N; i++) group[i] = i;
    }
    
    int get_group (int v) {
        return (group[v] == v) ? v : (group[v] = get_group(group[v]));
    }
    
    bool merge (int u, int v) {
        u = get_group(u);
        v = get_group(v);
        if(u == v) return false;
        group[u] = v;
        return true;
    }
};

void dfs (int u, int &l) {
    visited[u] = true;
    for(int e = 0; e < Gph[u].size(); e++) {
        int v = Gph[u][e];
        if(l != -1) DS::merge(l, v);
        l = v;
        if(!visited[v]) dfs(v, l);
    }
}

ll res, sz[N_];


int main() {
    scanf("%d%d", &N, &M);
    for(int i = 1; i <= M; i++) {
        int u, v; scanf("%d%d", &u, &v);
        Gph[u].pb(v);
    }
    
    DS::init();
    
    for(int i = 1; i <= N; i++) {
        int t = -1;
        if(!visited[i] && Gph[i].size() >= 2) dfs(i, t), visited[i] = false;
    }
    
    for(int i = 1; i <= N; i++) sz[DS::get_group(i)]++;
    for(int i = 1; i <= N; i++) res += sz[i] * (sz[i]-1);
    for(int i = 1; i <= N; i++) {
        for(int e = 0; e < Gph[i].size(); e++) {
            int j = Gph[i][e];
            int a = DS::get_group(i), b = DS::get_group(j);
            if(a != b) ++res;
        }
    }
    
    printf("%lld\n", res);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4820 KB Output is correct
2 Correct 0 ms 4820 KB Output is correct
3 Correct 0 ms 4820 KB Output is correct
4 Correct 0 ms 4820 KB Output is correct
5 Correct 0 ms 4820 KB Output is correct
6 Correct 0 ms 4820 KB Output is correct
7 Correct 0 ms 4820 KB Output is correct
8 Correct 0 ms 4820 KB Output is correct
9 Correct 0 ms 4820 KB Output is correct
10 Correct 0 ms 4820 KB Output is correct
11 Correct 0 ms 4820 KB Output is correct
12 Correct 0 ms 4820 KB Output is correct
13 Correct 0 ms 4820 KB Output is correct
14 Correct 0 ms 4820 KB Output is correct
15 Correct 0 ms 4820 KB Output is correct
16 Correct 0 ms 4820 KB Output is correct
17 Correct 0 ms 4820 KB Output is correct
18 Correct 0 ms 4820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4820 KB Output is correct
2 Correct 0 ms 4820 KB Output is correct
3 Correct 8 ms 4964 KB Output is correct
4 Correct 36 ms 5836 KB Output is correct
5 Correct 40 ms 5572 KB Output is correct
6 Correct 64 ms 6236 KB Output is correct
7 Correct 0 ms 4820 KB Output is correct
8 Correct 0 ms 4820 KB Output is correct
9 Correct 8 ms 4952 KB Output is correct
10 Correct 16 ms 5156 KB Output is correct
11 Correct 52 ms 6240 KB Output is correct
12 Correct 4 ms 4952 KB Output is correct
13 Correct 4 ms 4820 KB Output is correct
14 Correct 0 ms 4820 KB Output is correct
15 Correct 0 ms 4820 KB Output is correct
16 Correct 12 ms 5088 KB Output is correct
17 Correct 68 ms 6096 KB Output is correct
18 Correct 4 ms 4952 KB Output is correct
19 Correct 0 ms 5024 KB Output is correct
20 Correct 64 ms 6240 KB Output is correct
21 Correct 0 ms 4820 KB Output is correct
22 Correct 4 ms 4820 KB Output is correct
23 Correct 4 ms 4952 KB Output is correct
24 Correct 4 ms 4820 KB Output is correct
25 Correct 4 ms 4952 KB Output is correct
26 Correct 4 ms 4820 KB Output is correct
27 Correct 4 ms 4952 KB Output is correct
28 Correct 4 ms 4820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 5876 KB Output is correct
2 Correct 116 ms 8704 KB Output is correct
3 Correct 112 ms 8696 KB Output is correct
4 Correct 52 ms 6668 KB Output is correct
5 Correct 24 ms 6008 KB Output is correct
6 Correct 92 ms 7996 KB Output is correct
7 Correct 84 ms 7664 KB Output is correct
8 Correct 84 ms 7592 KB Output is correct
9 Correct 44 ms 6800 KB Output is correct
10 Correct 76 ms 7196 KB Output is correct
11 Correct 92 ms 8712 KB Output is correct
12 Correct 116 ms 9532 KB Output is correct
13 Correct 28 ms 7856 KB Output is correct
14 Correct 72 ms 6580 KB Output is correct
15 Correct 28 ms 6008 KB Output is correct
16 Correct 0 ms 4820 KB Output is correct
17 Correct 0 ms 4820 KB Output is correct
18 Correct 100 ms 7800 KB Output is correct
19 Correct 116 ms 7792 KB Output is correct
20 Correct 56 ms 6404 KB Output is correct
21 Correct 32 ms 5744 KB Output is correct
22 Correct 4 ms 4820 KB Output is correct
23 Correct 36 ms 5540 KB Output is correct
24 Correct 60 ms 6404 KB Output is correct
25 Correct 36 ms 7856 KB Output is correct
26 Correct 48 ms 12424 KB Output is correct
27 Correct 100 ms 10116 KB Output is correct
28 Correct 52 ms 7296 KB Output is correct
29 Correct 40 ms 7244 KB Output is correct
30 Correct 112 ms 8672 KB Output is correct
31 Correct 40 ms 7160 KB Output is correct
32 Correct 36 ms 7856 KB Output is correct
33 Correct 44 ms 7856 KB Output is correct
34 Correct 48 ms 6800 KB Output is correct
35 Correct 28 ms 7856 KB Output is correct
36 Correct 48 ms 9428 KB Output is correct
37 Correct 44 ms 7300 KB Output is correct