QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#318295 | #7177. Many Many Cycles | Minhho | WA | 15ms | 203080kb | C++17 | 2.2kb | 2024-01-30 22:46:56 | 2024-01-30 22:46:56 |
Judging History
answer
#define taskname "C"
#include <bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second
using namespace std;
const int maxn = 5050;
vector<ii> adj[maxn];
int dep[maxn], vis[maxn], mark[2*maxn], in[maxn], out[maxn], dis[maxn][maxn], cnt = 0, n, m;
array<int, 4> e[2*maxn];
void DFS(int u = 1, int par = 0)
{
vis[u] = 1;
in[u] = ++cnt;
for (auto [v, id]: adj[u]) if (v != par && !vis[v])
{
mark[id] = 1;
dep[v] = dep[u] + 1;
DFS(v, u);
}
out[u] = cnt;
}
/**
Build tree with DFS tree, no cross edge
**/
inline bool anc(int x, int y) {return in[x] <= in[y] && out[x] >= out[y];}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n>>m;
for (int i=1; i<=m; i++) cin>>e[i][1]>>e[i][2]>>e[i][0], e[i][3] = i;
vector<int> ve;
for (int i=1; i<=m; i++)
{
adj[e[i][1]].emplace_back(e[i][2], e[i][3]);
adj[e[i][2]].emplace_back(e[i][1], e[i][3]);
}
for (int i=1; i<=n; i++) if (!vis[i]) DFS(i, 0);
for (int i=1; i<=m; i++)
{
if (dep[e[i][1]] > dep[e[i][2]]) swap(e[i][1], e[i][2]);
if (!mark[i]) ve.emplace_back(i);
}
memset(dis, -1, sizeof dis);
for (int i=1; i<=n; i++)
{
dis[i][i] = 0;
queue<int> q;
q.emplace(i);
while (q.size())
{
int u = q.front(); q.pop();
for (auto [v, id]: adj[u])
{
int c = e[id][0];
if (dis[i][v] == -1) dis[i][v] = dis[i][u] + c, q.emplace(v);
}
}
}
int ans = 0;
for (int i=0; i<ve.size()-1; i++)
for (int j=i+1; j<ve.size(); j++)
{
auto [c1, u1, v1, id1] = e[ve[i]];
auto [c2, u2, v2, id2] = e[ve[j]];
if (dep[u1] > dep[u2]) swap(u1, u2), swap(v1, v2), swap(c1, c2);
if (anc(u1, u2) && anc(u2, v1)) ans = __gcd(ans, c1 + c2 + dis[u1][u2] + dis[v1][v2]);
}
for (int i: ve)
{
auto [c, u, v, id] = e[i];
ans = __gcd(ans, c + dis[u][v]);
}
cout<<ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 15ms
memory: 203080kb
input:
4 4 1 2 1 2 3 1 3 4 1 4 1 1
output:
2
result:
wrong answer expected '4', found '2'