답안 #2678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
2678 2013-07-30T14:49:33 Z tncks0121 간선 파괴 (GA5_destroy) C++
100 / 100
1808 ms 1880 KB
#include <stdio.h>
#include <memory.h>
#include <algorithm>

using namespace std;

const int N_ = 1005;
const int M_ = 200005;

int N, M, Q;
short A[M_], B[M_];

short parent[N_];
short rank[N_];

void init() { for(int i = 1; i <= N; i++) parent[i] = rank[i] = i; }

short get(short u) {
    int r = u;
    while(parent[r] != r) r = parent[r];
    while(u != r) {
        int p = parent[u];
        parent[u] = r;
        u = p;
    }
    return r;
}

bool merge (short a, short b) {
    a = get(a); b = get(b);
  if(rank[a] < rank[b]) {
    parent[a] = b;
  }else if(rank[a] > rank[b]) {
    parent[b] = a;
  }else {
    parent[b] = a;
    ++rank[a];
  }
    return a != b;
}

int R[N_*2], RN;

int main () {
    int i, j, k;

    scanf("%d%d", &N, &M);
    for(i = 1; i <= M; i++) scanf("%d%d", A+i, B+i);

    init();
    for(i = 1; i <= M; i++) if(merge(A[i], B[i])) R[++RN] = i;

    init();
    for(i = M; i > 0; i--) if(merge(A[i], B[i])) R[++RN] = i;

    scanf("%d", &Q);
    while(Q--) {
        int x, y, ret = 0;
        scanf("%d%d", &x, &y);

        init();
        for(i = 1; i <= RN; i++) {
            if(R[i] < x || y < R[i]) merge(A[R[i]], B[R[i]]);
        }
        for(i = 1; i <= N; i++) if(get(i) == i) ++ret;
        printf("%d\n", ret);
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1880 KB Output is correct
2 Correct 0 ms 1880 KB Output is correct
3 Correct 0 ms 1880 KB Output is correct
4 Correct 0 ms 1880 KB Output is correct
5 Correct 0 ms 1880 KB Output is correct
6 Correct 0 ms 1880 KB Output is correct
7 Correct 0 ms 1880 KB Output is correct
8 Correct 0 ms 1880 KB Output is correct
9 Correct 0 ms 1880 KB Output is correct
10 Correct 0 ms 1880 KB Output is correct
11 Correct 0 ms 1880 KB Output is correct
12 Correct 0 ms 1880 KB Output is correct
13 Correct 0 ms 1880 KB Output is correct
14 Correct 0 ms 1880 KB Output is correct
15 Correct 0 ms 1880 KB Output is correct
16 Correct 0 ms 1880 KB Output is correct
17 Correct 0 ms 1880 KB Output is correct
18 Correct 0 ms 1880 KB Output is correct
19 Correct 0 ms 1880 KB Output is correct
20 Correct 0 ms 1880 KB Output is correct
21 Correct 0 ms 1880 KB Output is correct
22 Correct 0 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 1880 KB Output is correct
2 Correct 36 ms 1880 KB Output is correct
3 Correct 44 ms 1880 KB Output is correct
4 Correct 52 ms 1880 KB Output is correct
5 Correct 44 ms 1880 KB Output is correct
6 Correct 388 ms 1880 KB Output is correct
7 Correct 352 ms 1880 KB Output is correct
8 Correct 396 ms 1880 KB Output is correct
9 Correct 40 ms 1880 KB Output is correct
10 Correct 40 ms 1880 KB Output is correct
11 Correct 40 ms 1880 KB Output is correct
12 Correct 44 ms 1880 KB Output is correct
13 Correct 44 ms 1880 KB Output is correct
14 Correct 44 ms 1880 KB Output is correct
15 Correct 40 ms 1880 KB Output is correct
16 Correct 44 ms 1880 KB Output is correct
17 Correct 36 ms 1880 KB Output is correct
18 Correct 32 ms 1880 KB Output is correct
19 Correct 16 ms 1880 KB Output is correct
20 Correct 16 ms 1880 KB Output is correct
21 Correct 4 ms 1880 KB Output is correct
22 Correct 4 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1540 ms 1880 KB Output is correct
2 Correct 1332 ms 1880 KB Output is correct
3 Correct 1808 ms 1880 KB Output is correct
4 Correct 1596 ms 1880 KB Output is correct
5 Correct 1488 ms 1880 KB Output is correct
6 Correct 1440 ms 1880 KB Output is correct
7 Correct 1720 ms 1880 KB Output is correct
8 Correct 1632 ms 1880 KB Output is correct
9 Correct 1580 ms 1880 KB Output is correct
10 Correct 1532 ms 1880 KB Output is correct
11 Correct 1488 ms 1880 KB Output is correct
12 Correct 1476 ms 1880 KB Output is correct
13 Correct 1700 ms 1880 KB Output is correct
14 Correct 1548 ms 1880 KB Output is correct
15 Correct 1492 ms 1880 KB Output is correct
16 Correct 1460 ms 1880 KB Output is correct
17 Correct 1268 ms 1880 KB Output is correct
18 Correct 1232 ms 1880 KB Output is correct
19 Correct 536 ms 1880 KB Output is correct
20 Correct 696 ms 1880 KB Output is correct
21 Correct 164 ms 1880 KB Output is correct
22 Correct 152 ms 1880 KB Output is correct