QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#450921 | #6830. Just Some Bad Memory | PetroTarnavskyi# | WA | 1ms | 3712kb | C++20 | 2.4kb | 2024-06-22 19:35:45 | 2024-06-22 19:35:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 1 << 17;
template<typename T>
void updMin(T& a, T b)
{
a = min(a, b);
}
template<typename T>
void updMax(T& a, T b)
{
a = max(a, b);
}
VI g[N];
int d[N];
int delta[N];
bool oddCycle, evenCycle, triangle, bigOddCycle;
int szComp, maxDeg;
int dfs(int v, int p)
{
szComp++;
updMax(maxDeg, SZ(g[v]));
int subtreeSum = 0;
for (int to : g[v])
{
if (to == p)
continue;
if (d[to] != -1)
{
if ((d[v] - d[to]) % 2 == 0)
{
oddCycle = true;
if (d[v] - d[to] == 2)
triangle = true;
else
bigOddCycle = true;
delta[v]++;
delta[to]--;
}
else
{
evenCycle = true;
}
}
else
{
d[to] = d[v] + 1;
subtreeSum += dfs(to, v);
}
}
subtreeSum += delta[v];
if (subtreeSum > 1)
evenCycle = true;
return subtreeSum;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
if (n < 4)
{
cout << "-1\n";
return 0;
}
if (m <= 1)
{
cout << 5 - m << "\n";
return 0;
}
FOR(i, 0, m)
{
int u, v;
cin >> u >> v;
u--;
v--;
g[u].PB(v);
g[v].PB(u);
}
fill(d, d + n, -1);
int ans = 3;
bool oddCycleInGraph = false;
bool evenCycleInGraph = false;
bool triangleInGraph = false;
bool longChainInGraph = false;
int maxComp = 0;
FOR(i, 0, n)
{
if (d[i] == -1)
{
oddCycle = evenCycle = triangle = bigOddCycle = false;
szComp = 0;
maxDeg = 0;
d[i] = 0;
dfs(i, -1);
if (evenCycle || bigOddCycle)
updMin(ans, 1);
if (triangle && szComp > 3)
updMin(ans, 1);
triangleInGraph |= triangle;
longChainInGraph |= szComp > 3 && maxDeg < szComp - 1;
updMax(maxComp, szComp);
oddCycleInGraph |= oddCycle;
evenCycleInGraph |= evenCycle;
}
}
if (oddCycleInGraph && evenCycleInGraph)
updMin(ans, 0);
if (triangleInGraph && longChainInGraph)
updMin(ans, 1);
if (triangleInGraph)
updMin(ans, 2);
if (maxComp >= 4)
updMin(ans, 2);
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3636kb
input:
3 3 1 2 2 3 1 3
output:
-1
result:
ok "-1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
4 0
output:
5
result:
ok "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
5 4 1 2 2 3 3 4 4 5
output:
2
result:
ok "2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3644kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 3 4
output:
0
result:
ok "0"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3632kb
input:
4 4 1 2 2 3 3 4 4 1
output:
1
result:
ok "1"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
7 7 1 2 2 3 3 4 4 1 5 6 6 7 7 5
output:
0
result:
ok "0"
Test #7:
score: -100
Wrong Answer
time: 1ms
memory: 3712kb
input:
4 3 1 2 2 3 3 1
output:
1
result:
wrong answer 1st words differ - expected: '2', found: '1'