Submission #109120

# Submission time Handle Problem Language Result Execution time Memory
109120 2019-05-04T12:38:31 Z ffresh 전압 (JOI14_voltage) C++17
0 / 100
188 ms 16836 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 1e5+15;
const int M = 2e5+15;

int dp[N],dp2[N],add[N],add2[N];
vector<int> adj[N];

int a[M],b[M];

int depth[N],parent[N];

int numOfOddCycle = 0;

stack<int> st;
bool ban[M];
vector<pair<int,int> > g;
void dfs(int node,int prevEdge,int curDep) {
    depth[node] = curDep;
    st.push(node);
    for(int i=0;i<adj[node].size(); ++i) {
        int id = adj[node][i];
        if(id == prevEdge) {
            continue;
        }
        int nextNode = a[id]^b[id]^node;
        if(depth[nextNode]==0 ){
            parent[nextNode] = node;
            dfs(nextNode,id,curDep+1);
        } else if(depth[nextNode] <depth[node]){
            if(depth[nextNode]%2== depth[node]%2) {
                //printf("%d %d\n", depth[nextNode],depth[node]);
                ++add2[node];
                ban[id] = true;
                --add2[nextNode];
                ++numOfOddCycle;
            } else {
                //printf("%d %d\n", node,nextNode);
                ++add[node];
                --add[nextNode];
            }
        }
    }
}

bool vis[N];

int color[N];
bool dfs2(int node,int c) {
    vis[node] = true;
    color[node] = c;
    for(int i=0;i<adj[node].size();++i) {
        int id = adj[node][i];
        if(ban[id]) {
            continue;
        }
        int ch = a[id]^b[id]^node;
        if(!vis[ch]) {
            dfs2(ch,c^1);
        } else {
            if(color[node]==color[ch]){
                return false;
            }
        }
    }
    return true;
}

void solve() {
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;++i) {
        scanf("%d%d",&a[i],&b[i]);
        adj[a[i]].push_back(i);
        adj[b[i]].push_back(i);
    }
    for(int i=1;i<=n;++i ){
        if(depth[i]==0) {
            dfs(i,-1,1);
        }
    }
    int ret = 0;
    //printf("%d\n", numOfOddCycle);
    while(!st.empty()) {
        int curNode = st.top();
        st.pop();
        dp2[curNode] += add2[curNode];
        //printf("%d %d %d %d\n", curNode,parent[curNode], dp[curNode],dp2[curNode]);
        if(dp2[curNode]== numOfOddCycle && dp[curNode]==0 && parent[curNode]!=0 ) {
            //printf("%d\n", curNode);
            ++ret;
        }
        dp[curNode] += add[curNode];
        dp2[parent[curNode]] += dp2[curNode];
        dp[parent[curNode]] += dp[curNode];
    }
    if(numOfOddCycle==1) {
        bool go = true;
        for(int i=1;i<=n;++i) {
            if(!vis[i]) {
                go &= dfs2(i,0);
            }
        }
        if(go) {
            ++ret;
        }
    }

    printf("%d\n", ret);
}

int main() {
    //freopen("input.txt","r",stdin);
    solve();
}

Compilation message

voltage.cpp: In function 'void dfs(int, int, int)':
voltage.cpp:23:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<adj[node].size(); ++i) {
                 ~^~~~~~~~~~~~~~~~~
voltage.cpp: In function 'bool dfs2(int, int)':
voltage.cpp:54:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<adj[node].size();++i) {
                 ~^~~~~~~~~~~~~~~~~
voltage.cpp: In function 'void solve()':
voltage.cpp:73: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:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&a[i],&b[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 60 ms 9020 KB Output is correct
2 Correct 147 ms 13068 KB Output is correct
3 Correct 65 ms 9556 KB Output is correct
4 Correct 188 ms 15172 KB Output is correct
5 Correct 11 ms 3712 KB Output is correct
6 Correct 96 ms 11128 KB Output is correct
7 Correct 157 ms 16836 KB Output is correct
8 Incorrect 143 ms 16516 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 9036 KB Output is correct
2 Correct 56 ms 16392 KB Output is correct
3 Incorrect 83 ms 16324 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 64 ms 10516 KB Output isn't correct
2 Halted 0 ms 0 KB -