# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
48288 | choikiwon | 전압 (JOI14_voltage) | C++17 | 86 ms | 12732 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<bits/stdc++.h>
using namespace std;
const int MN = 100010;
int N, M;
vector<int> adj[MN], U, V;
int tin[MN], timer, cnt;
priority_queue<int> 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) {
dfs0(v, e);
}
else if(tin[v] < tin[u]) {
if(tin[v] % 2 == tin[u] % 2) 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);
}
}
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 && tin[v] < tin[u]) {
if(tin[v] % 2 == tin[u] % 2) pq1.push(tin[v]);
else pq2.push(tin[v]);
}
}
while(!pq1.empty() && pq1.top() == tin[u]) pq1.pop();
while(!pq2.empty() && pq2.top() == tin[u]) pq2.pop();
if(u && (int)pq1.size() == cnt && !pq2.size()) ans++;
}
int main() {
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++) {
int u, v; scanf("%d %d", &u, &v);
u--; v--;
adj[u].push_back(U.size());
adj[v].push_back(U.size());
U.push_back(u);
V.push_back(v);
}
memset(tin, -1, sizeof(tin));
for(int i = 0; i < N; i++) if(tin[i] == -1) {
dfs0(i, -1);
}
memset(tin, -1, sizeof(tin));
for(int i = 0; i < N; i++) if(tin[i] == -1) {
dfs1(i, -1);
}
if(cnt == 1) ans++;
printf("%d", ans);
}
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... |