QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606724 | #8941. Even or Odd Spanning Tree | UESTC_NLNS# | WA | 108ms | 17276kb | C++14 | 2.2kb | 2024-10-03 11:48:57 | 2024-10-03 11:49:00 |
Judging History
answer
#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define ll long long
using namespace std;
const int N=2e5+5,M=5e5+5,INF=0x3f3f3f3f;
struct node{
int u,v,w;
bool operator < (const node &x) const{
return w<x.w;
}
}edg[M];
int T,n,m,a,b,c,dep[N],f[N],fa[N][20],mx[2][N][20];
ll tmp,ans[2];
vector<pii > e[N];
bool vis[N];
int getfa(int x) {return f[x]==x?x:(f[x]=getfa(f[x]));}
int merge(int x,int y,int w)
{
int fx=getfa(x),fy=getfa(y);
if(fx==fy) return 0;
e[x].push_back({y,w});
e[y].push_back({x,w});
f[fy]=fx;
tmp+=w;
return 1;
}
void Dfs(int x,int pre,int w)
{
dep[x]=dep[pre]+1;fa[x][0]=pre;mx[w&1][x][0]=w;
for(int i=1;(1<<i)<dep[x];i++)
{
fa[x][i]=fa[fa[x][i-1]][i-1];
mx[0][x][i]=max(mx[0][x][i-1],mx[0][fa[x][i-1]][i-1]);
mx[1][x][i]=max(mx[1][x][i-1],mx[1][fa[x][i-1]][i-1]);
}
for(auto v:e[x])
{
if(v.first==pre) continue;
Dfs(v.first,x,v.second);
}
return;
}
int LCA(int x,int y,int tp)
{
if(dep[x]>dep[y]) swap(x,y);
int res=0;
for(int i=19;i>=0;i--) if(dep[fa[y][i]]>=dep[x]) res=max(res,mx[tp][y][i]),y=fa[y][i];
for(int i=19;i>=0;i--) if(fa[x][i]!=fa[y][i]) res=max(res,max(mx[tp][x][i],mx[tp][y][i])),x=fa[x][i],y=fa[y][i];
res=max(res,max(mx[tp][x][0],mx[tp][y][0]));
return res;
}
void solve()
{
int cnt=0,s;ans[0]=ans[1]=-1;
sort(edg+1,edg+m+1);tmp=0;
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++)
{
if(merge(edg[i].u,edg[i].v,edg[i].w)) cnt++,vis[i]=true;
if(cnt==n-1) break;
}
if(cnt!=n-1) return;
ans[tmp&1]=tmp;ans[(tmp&1)^1]=INF;
Dfs(1,0,0);
for(int i=1;i<=m;i++)
{
if(!vis[i])
{
s=LCA(edg[i].u,edg[i].v,(edg[i].w&1)^1);
if(s) ans[(tmp&1)^1]=min(ans[(tmp&1)^1],tmp+edg[i].w-s);
}
}
if(ans[(tmp&1)^1]==INF) ans[(tmp&1)^1]=-1;
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=m;i++) cin>>edg[i].u>>edg[i].v>>edg[i].w;
solve();
cout<<ans[0]<<" "<<ans[1]<<"\n";
for(int i=1;i<=n;i++) e[i].clear();
for(int i=1;i<=m;i++) vis[i]=false;
for(int i=1;i<=n;i++) for(int j=0;j<20;j++) mx[0][i][j]=mx[1][i][j]=0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17276kb
input:
3 2 1 1 2 5 3 1 1 3 1 4 4 1 2 1 1 3 1 1 4 1 2 4 2
output:
-1 5 -1 -1 4 3
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 108ms
memory: 15964kb
input:
10000 16 50 1 2 649251632 2 3 605963017 3 4 897721528 4 5 82446151 5 6 917109168 6 7 79647183 7 8 278566178 7 9 573504723 3 10 679859251 8 11 563113083 1 12 843328546 10 13 594049160 11 14 997882839 7 15 569431750 2 16 244494799 16 7 960172373 13 4 317888322 13 3 446883598 9 3 678142319 5 8 43208692...
output:
3140067932 -1 1262790434 -1 1263932600 -1 -1 1180186165 2248358640 -1 -1 3738936375 -1 1058677117 -1 3402127725 -1 1187873655 1395482806 -1 3456885934 -1 3943951826 -1 2479987500 -1 2909126794 -1 2483103706 -1 -1 1824129583 3769162572 -1 548190194 537074351 -1 2475481185 1133556832 -1 -1 3120149981 ...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '-1'