# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
25767 | kriii | 전압 (JOI14_voltage) | C++14 | 229 ms | 15160 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> G[100100];
int N,M,X[200200],Y[200200],C[200200];
int D[100100],U[100100];
int gety(int x, int i)
{
return X[i] == x ? Y[i] : X[i];
}
int bridge, badback, badmx, badmxcnt, badcnt[100100], goodcnt[100100];
void dfs(int x)
{
U[x] = D[x];
for (auto &i : G[x]) if (!C[i]){
C[i] = 1;
int y = gety(x,i);
if (D[y] == 0){
D[y] = D[x] + 1;
dfs(y);
badcnt[x] += badcnt[y];
goodcnt[x] += goodcnt[y];
if (U[y] == D[y]) bridge++;
if (U[x] > U[y]) U[x] = U[y];
}
else{
if (D[x] % 2 == D[y] % 2){
badback++;
badcnt[x]++; badcnt[y]--;
}
else{
goodcnt[x]++; goodcnt[y]--;
}
if (U[x] > D[y]) U[x] = D[y];
}
}
if (goodcnt[x] == 0){
if (badmx < badcnt[x]) badmx = badcnt[x], badmxcnt = 1;
else if (badmx == badcnt[x]) badmxcnt++;
}
}
int main()
{
scanf ("%d %d",&N,&M);
for (int i=0;i<M;i++){
scanf ("%d %d",&X[i],&Y[i]);
G[X[i]].push_back(i);
G[Y[i]].push_back(i);
}
vector<int> res,bad;
for (int i=1;i<=N;i++) if (D[i] == 0){
bridge = badback = 0;
badmx = badmxcnt = 0;
D[i] = 1;
dfs(i);
int r = 0;
if (badback == 0) r += bridge;
else if (badback == 1) r++;
if (badback >= 1){
if (badmx == badback) r += badmxcnt;
}
res.push_back(r);
bad.push_back(badback >= 1);
}
int ans = 0;
int bads = 0;
for (int x : bad) bads += x;
if (bads == 0){
for (int x : res) ans += x;
}
else if (bads == 1){
for (int i=0;i<bad.size();i++) if (bad[i]) ans += res[i];
}
printf ("%d\n",ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |