이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e6 + 5;
static int n;
static vector<int> ciclu;
static vector<int> cen, g3, trk;
static vector<int> vec[nmax + 1];
static int grad[nmax + 1];
struct Str {
bool valid;
int c1[nmax + 1], c2[nmax + 1];
int t[nmax + 1], g[nmax + 1];
void init () {
valid = 1;
for (int i = 0; i < n; ++ i) {
t[i] = c1[i] = c2[i] = i;
}
}
inline int find_tata (int x) {
if (x == t[x]) return x;
return t[x] = find_tata(t[x]);
}
void link (int A, int B, int type = -1) {
++ g[A]; ++ g[B];
if (type == 1) {
vec[A].push_back(B);
vec[B].push_back(A);
}
if (valid == 0)
return ;
if (g[A] < 3 && g[B] < 3) {
int la = find_tata(A);
int lb = find_tata(B);
if (la == lb) { // apare ciclu
if (type == 1 && (c1[la] == A && c2[la] == B || c1[la] == B && c2[la] == A))
c1[la] = c2[la] = -1;
else
valid = 0;
} else {
t[lb] = la;
if (type == 1)
grad[la] += grad[lb];
if (A == c1[la]) {
c1[la] = c1[lb] + c2[lb] - B;
} else {
c2[la] = c1[lb] + c2[lb] - B;
}
}
} else
valid = 0;
}
} root, centered, s3[12];
static vector<pair<int, int>> e;
static map<pair<int, int>, bool> mp;
void Init(int N_) {
n = N_;
for (int i = 0; i < n; ++ i)
grad[i] = 1;
root.init();
}
bool nuexista (int i) {
bool ok = 1;
for (auto j : trk)
if (i == j) ok = 0;
return ok;
}
void Link(int A, int B) {
e.push_back({A, B});
mp[{A, B}] = mp[{B, A}] = 1;
if (root.g[A] == 1 && root.g[B] == 1 && root.find_tata(A) == root.find_tata(B)) {
ciclu.push_back(grad[root.find_tata(A)]);
}
root.link(A, B, 1);
int oldcen = cen.size();
int old3 = trk.size();
if (cen.size() == 0 && (root.g[A] >= 3 || root.g[B] >= 3)) {
if (g3.size() > 3)
return ;
if (root.g[A] == 3) {
g3.push_back(A);
if (nuexista(A))
trk.push_back(A);
for (auto i : vec[A]) {
if (nuexista(i))
trk.push_back(i);
}
}
if (root.g[B] == 3) {
g3.push_back(B);
if (nuexista(B))
trk.push_back(B);
for (auto i : vec[B]) {
if (nuexista(i))
trk.push_back(i);
}
}
if (root.g[A] > 3) cen.push_back(A);
if (root.g[B] > 3) cen.push_back(B);
}
if (g3.size() == 4) {
for (auto i : g3) {
bool ok = 1;
for (auto j : vec[i]) {
if (vec[j].size() != 3)
ok = 0;
}
if (ok == 1)
cen.push_back(i);
}
}
// dc nu exista cen, at tin pentru cele 3 g3 uri cum arata fara fiecare
if (cen.size() == 0) {
if (g3.size() > 3)
return ;
for (int i = old3; i < (int)trk.size(); ++ i) {
s3[i].init();
for (auto j : e)
if (j.first != trk[i] && j.second != trk[i])
s3[i].link(j.first, j.second);
}
for (int i = 0; i < old3; ++ i)
if (A != trk[i] && B != trk[i])
s3[i].link(A, B);
}
if (oldcen == 0 && cen.size() == 1) { // a aparut bossu refac tot
centered.init();
for (auto i : e)
if (i.first != cen[0] && i.second != cen[0])
centered.link(i.first, i.second);
}
if (cen.size() == 1 && oldcen == 1)
if (A != cen[0] && B != cen[0])
centered.link(A, B);
}
int CountCritical() {
if (ciclu.size() > 1 || cen.size() > 1) {
return 0;
}
if (g3.size() == 0) {
if (ciclu.size() == 0)
return n;
return ciclu[0];
} else {
if (cen.size() == 0 && g3.size() > 3) // daca sunt 3+ trebuie sa aiba un centru
return 0;
if (cen.size()) {
return centered.valid;
} else {
int cnt = 0;
for (int i = 0; i < (int)trk.size(); ++ i) {
cnt += s3[i].valid;
}
return cnt;
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
rings.cpp: In member function 'void Str::link(int, int, int)':
rings.cpp:47:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (type == 1 && (c1[la] == A && c2[la] == B || c1[la] == B && c2[la] == A))
~~~~~~~~~~~~^~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |