Submission #581736

# Submission time Handle Problem Language Result Execution time Memory
581736 2022-06-23T05:26:34 Z 박상훈(#8364) 전압 (JOI14_voltage) C++14
10 / 100
105 ms 17104 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);
    for (auto &v:adj[s]) if (v!=pa){
        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, 0);
    int t = dfs_cyc(1), len = -1;
    for (int i=(int)cyc.size()-1;i>=0;i--) if (cyc[i]==t){
        len = (int)cyc.size() - i + 1;
    }
    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:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
voltage.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 67 ms 2644 KB Output is correct
2 Correct 56 ms 2684 KB Output is correct
3 Correct 46 ms 2688 KB Output is correct
4 Correct 32 ms 2644 KB Output is correct
5 Correct 98 ms 2732 KB Output is correct
6 Correct 97 ms 2644 KB Output is correct
7 Correct 103 ms 2736 KB Output is correct
8 Correct 81 ms 2712 KB Output is correct
9 Correct 85 ms 2644 KB Output is correct
10 Correct 103 ms 2728 KB Output is correct
11 Correct 105 ms 2728 KB Output is correct
12 Correct 99 ms 2732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 14676 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 40 ms 14760 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 66 ms 17104 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -