답안 #48294

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48294 2018-05-11T12:46:13 Z choikiwon 전압 (JOI14_voltage) C++17
100 / 100
283 ms 55644 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 100010;

int N, M;
vector<int> adj[MN], U, V;
int tin[MN], tout[MN], col[MN], timer, cnt;

struct Fenwick {
    vector<int> tree;
    void init() {
        tree = vector<int>(N + 1, 0);
    }
    void upd(int idx, int val) {
        for(int i = idx + 1; i <= N; i += (i & -i)) tree[i] += val;
    }
    int quer(int a) {
        int ret = 0;
        for(int i = a + 1; i >= 1; i -= (i & -i)) ret += tree[i];
        return ret;
    }
    int quer(int a, int b) {
        return quer(b) - quer(a - 1);
    }
} bit1, bit2;
priority_queue<pii> pq1, pq2;
int ans;

void dfs0(int u, int fe) {
    tin[u] = timer++;
    for(int i = 0; i < adj[u].size(); i++) {
        int e = adj[u][i];
        int v = U[e] + V[e] - u;
        if(e == fe) continue;
        if(tin[v] == -1) {
            col[v] = col[u] ^ 1;
            dfs0(v, e);
        }
        else if(tin[v] < tin[u]) {
            if(col[v] == col[u]) cnt++;
        }
    }
}

void dfs1(int u, int fe) {
    tin[u] = timer++;
    for(int i = 0; i < adj[u].size(); i++) {
        int e = adj[u][i];
        int v = U[e] + V[e] - u;
        if(e == fe) continue;
        if(tin[v] == -1) {
            dfs1(v, e);
        }
        else if(tin[v] < tin[u]) {
            if(col[v] == col[u]) pq1.push({ tin[v], tin[u] }), bit1.upd(tin[u], 1);
            else pq2.push({ tin[v], tin[u] }), bit2.upd(tin[u], 1);
        }
    }
    tout[u] = timer;

    while(!pq1.empty() && pq1.top().first >= tin[u]) bit1.upd(pq1.top().second, -1), pq1.pop();
    while(!pq2.empty() && pq2.top().first >= tin[u]) bit2.upd(pq2.top().second, -1), pq2.pop();
    if(fe != -1 && bit1.quer(tin[u], tout[u] - 1) == cnt && !bit2.quer(tin[u], tout[u] - 1)) ans++;
}

int main() {
    scanf("%d %d", &N, &M);

    int loop = 0;
    for(int i = 0; i < M; i++) {
        int u, v; scanf("%d %d", &u, &v);
        u--; v--;

        if(u == v) {
            loop++;
            continue;
        }

        adj[u].push_back(U.size());
        adj[v].push_back(U.size());
        U.push_back(u);
        V.push_back(v);
    }

    if(loop >= 2) {
        printf("0");
        return 0;
    }

    memset(tin, -1, sizeof(tin));
    for(int i = 0; i < N; i++) if(tin[i] == -1) {
        dfs0(i, -1);
    }

    if(loop) {
        if(!cnt) printf("1");
        else printf("0");
        return 0;
    }

    memset(tin, -1, sizeof(tin));
    timer = 0;
    bit1.init();
    bit2.init();
    for(int i = 0; i < N; i++) if(tin[i] == -1) {
        dfs1(i, -1);
    }

    if(cnt == 1) ans++;
    printf("%d", ans);
}

Compilation message

voltage.cpp: In function 'void dfs0(int, int)':
voltage.cpp:34:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
voltage.cpp: In function 'void dfs1(int, int)':
voltage.cpp:50:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
voltage.cpp: In function 'int main()':
voltage.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &M);
     ~~~~~^~~~~~~~~~~~~~~~~
voltage.cpp:74:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u, v; scanf("%d %d", &u, &v);
                   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3192 KB Output is correct
2 Correct 5 ms 3192 KB Output is correct
3 Correct 4 ms 3248 KB Output is correct
4 Correct 4 ms 3284 KB Output is correct
5 Correct 5 ms 3360 KB Output is correct
6 Correct 5 ms 3360 KB Output is correct
7 Correct 5 ms 3376 KB Output is correct
8 Correct 8 ms 3440 KB Output is correct
9 Correct 5 ms 3472 KB Output is correct
10 Correct 5 ms 3616 KB Output is correct
11 Correct 5 ms 3616 KB Output is correct
12 Correct 5 ms 3616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 9356 KB Output is correct
2 Correct 139 ms 13628 KB Output is correct
3 Correct 67 ms 13628 KB Output is correct
4 Correct 111 ms 15128 KB Output is correct
5 Correct 11 ms 15128 KB Output is correct
6 Correct 107 ms 15128 KB Output is correct
7 Correct 110 ms 17980 KB Output is correct
8 Correct 121 ms 19232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 19232 KB Output is correct
2 Correct 52 ms 19320 KB Output is correct
3 Correct 55 ms 19320 KB Output is correct
4 Correct 4 ms 19320 KB Output is correct
5 Correct 82 ms 19320 KB Output is correct
6 Correct 87 ms 19320 KB Output is correct
7 Correct 115 ms 19320 KB Output is correct
8 Correct 123 ms 20116 KB Output is correct
9 Correct 114 ms 20956 KB Output is correct
10 Correct 103 ms 20956 KB Output is correct
11 Correct 80 ms 20956 KB Output is correct
12 Correct 94 ms 21496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 22372 KB Output is correct
2 Correct 89 ms 28140 KB Output is correct
3 Correct 6 ms 28140 KB Output is correct
4 Correct 135 ms 28140 KB Output is correct
5 Correct 118 ms 28140 KB Output is correct
6 Correct 124 ms 28140 KB Output is correct
7 Correct 211 ms 30396 KB Output is correct
8 Correct 231 ms 33876 KB Output is correct
9 Correct 187 ms 34248 KB Output is correct
10 Correct 218 ms 38996 KB Output is correct
11 Correct 225 ms 38996 KB Output is correct
12 Correct 257 ms 43864 KB Output is correct
13 Correct 166 ms 43864 KB Output is correct
14 Correct 283 ms 49196 KB Output is correct
15 Correct 278 ms 51076 KB Output is correct
16 Correct 192 ms 52200 KB Output is correct
17 Correct 202 ms 52320 KB Output is correct
18 Correct 181 ms 55644 KB Output is correct