답안 #581741

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581741 2022-06-23T05:32:27 Z 박상훈(#8364) 전압 (JOI14_voltage) C++14
10 / 100
68 ms 14884 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[100100];
pair<int, int> E[200200];
int n, m, visited[200200];

bool dfs_col(int s, int c = 0){
    visited[s] = c;
    for (auto &v:adj[s]){
        if (visited[v]<0){
            if (!dfs_col(v, c^1)) return 0;
        }
        else if (visited[v]==c) return 0;
    }
    return 1;
}

void naive(){
    int ans = 0;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++) adj[j].clear();
        for (int j=1;j<=m;j++) if (i!=j){
            adj[E[j].first].push_back(E[j].second);
            adj[E[j].second].push_back(E[j].first);
        }

        fill(visited+1, visited+n+1, -1);
        visited[E[i].first] = visited[E[i].second] = 0;
        bool flag = 1;
        flag &= dfs_col(E[i].first);
        flag &= dfs_col(E[i].second);

        for (int j=1;j<=n;j++) if (visited[j]<0) flag &= dfs_col(j);
        if (flag) ans++;
    }
    printf("%d\n", ans);
    exit(0);
}

vector<int> cyc;
int dfs_cyc(int s, int pa = -1){
    visited[s] = 1;
    cyc.push_back(s);

    int pacnt = 0;
    for (auto &v:adj[s]){
        if (v==pa){
            pacnt++;
            if (pacnt==1) continue;
        }

        if (visited[v]>=0){
            return v;
        }
        int tmp = dfs_cyc(v, s);
        if (tmp>=0) return tmp;
    }
    cyc.pop_back();
    return -1;
}

void sol2(){
    fill(visited+1, visited+n+1, -1);
    int t = dfs_cyc(1), len = -1;

    assert(t!=-1);
    for (int i=(int)cyc.size()-1;i>=0;i--) if (cyc[i]==t){
        len = (int)cyc.size() - i;
        break;
    }
    //for (auto &x:cyc) printf(" %d", x);
    //printf(" / %d\n", t);
    assert(len!=-1);

    if (len%2==0) printf("%d\n", m - len);
    else printf("%d\n", len);
}

int main(){
    scanf("%d %d", &n, &m);
    for (int i=1;i<=m;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        adj[x].push_back(y);
        adj[y].push_back(x);
        E[i] = {x, y};
    }

    //if (n<=1000) naive();

    sol2();
    return 0;
}

Compilation message

voltage.cpp: In function 'int main()':
voltage.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
voltage.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 7316 KB Output is correct
2 Correct 54 ms 11976 KB Output is correct
3 Correct 39 ms 8460 KB Output is correct
4 Correct 68 ms 13392 KB Output is correct
5 Correct 6 ms 3412 KB Output is correct
6 Correct 51 ms 10360 KB Output is correct
7 Correct 66 ms 14884 KB Output is correct
8 Correct 64 ms 14796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 33 ms 14744 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 50 ms 8476 KB Output isn't correct
2 Halted 0 ms 0 KB -