답안 #6918

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
6918 2014-07-10T12:44:59 Z tncks0121 우호 조약 체결 (JOI14_friends) C++
0 / 100
116 ms 8712 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);
    }
    
    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 Incorrect 0 ms 4820 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 4820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 5876 KB Output is correct
2 Incorrect 116 ms 8712 KB Output isn't correct
3 Halted 0 ms 0 KB -