QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#250483 | #7177. Many Many Cycles | masterhuang | WA | 1ms | 3996kb | C++20 | 1.4kb | 2023-11-13 10:44:12 | 2023-11-13 10:44:12 |
Judging History
answer
#include<bits/stdc++.h>
#define P pair<int,int>
#define fi first
#define se second
#define LL long long
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=5005;
int n,m,fa[N],W[N],lca[N][N];LL d[N],G;bool v[N],vv[N],vis[N];
struct node{int u,v,w;}e[N<<1];
vector<P>g[N];vector<node>h;basic_string<int>son[N];
void dfs(int x)
{
v[x]=1;
for(auto [y,w]:g[x]) if(!v[y])
{
fa[y]=x,d[y]=d[x]+(W[y]=w),dfs(y);
for(int i:son[y]) for(int j:son[x]) lca[i][j]=lca[j][i]=x;
for(int i:son[y]) son[x]+=i;
}
for(int i:son[x]) lca[x][i]=lca[i][x]=x;lca[x][x]=x;son[x]+=x;
}
signed main()
{
// fr(1)
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m;
for(int i=1,u,v,w;i<=m;i++) cin>>u>>v>>w,g[u].push_back({v,w}),g[v].push_back({u,w}),e[i]={u,v,w};
for(int k=1;k<=n;k++) if(!v[k])
{
memcpy(vv,v,sizeof(v));dfs(k);
for(int i=1;i<=n;i++) vv[i]^=v[i];h.clear();
for(int i=1;i<=m;i++) if(vv[e[i].u]) h.push_back(e[i]);
for(int i=0;i<h.size();i++)
{
auto [u,v,w]=h[i];(d[u]>d[v])&&(swap(u,v),1);if(fa[v]==u) continue;
for(int j=v;j^u;j=fa[j]) vis[j]=1;vis[u]=1;G=__gcd(G,d[v]-d[u]+w);
for(int j=0;j<h.size();j++) if(i^j)
{
auto [U,V,W]=h[j];(d[U]>d[V])&&(swap(U,V),1);V=lca[v][V];
int up=d[u]>d[U]?u:U;if(fa[V]==U||d[up]>=d[V]) continue;
G=__gcd(G,(d[up]-d[V])<<1);
}
for(int j=v;j^u;j=fa[j]) vis[j]=0;vis[u]=0;
}
}
return cout<<G,0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3996kb
input:
4 4 1 2 1 2 3 1 3 4 1 4 1 1
output:
4
result:
ok answer is '4'
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3940kb
input:
4 5 1 2 1 1 3 2 1 4 1 2 3 1 3 4 1
output:
-4
result:
wrong answer expected '4', found '-4'