QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#580735 | #8941. Even or Odd Spanning Tree | Forever_Young# | WA | 117ms | 11976kb | C++23 | 2.8kb | 2024-09-21 23:23:32 | 2024-09-21 23:23:43 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define stack stck
using namespace std;
#define inf 998244353
#define N 210000
#define M 510000
int n,m;
struct Edge
{
int x,y,w;
friend bool operator <(Edge x,Edge y)
{
return x.w<y.w;
}
}a[M];
int father[N];
vector<pair<int,int> > g[N];
int pre[N][20],f[2][N][20];
int dep[N];
void dfs(int x,int fa,int prew,int Dep)
{
dep[x]=Dep;
f[0][x][0]=f[1][x][0]=-2147483647;
pre[x][0]=fa;
if (x!=1)
{ if (prew&1)f[1][x][0]=prew; else f[0][x][0]=prew; }
for(int i=1;i<=20;i++)pre[x][i]=pre[pre[x][i-1]][i-1];
for(int i=1;i<=20;i++)
for(int j=0;j<=1;j++)
f[j][x][i]=max(f[j][x][i-1],f[j][pre[x][i-1]][i-1]);
for(auto p:g[x])
if (p.first!=fa)dfs(p.first,x,p.second,Dep+1);
}
int get(int x,int y,int sign)
{
int ret=-2147483647;
if (dep[x]<dep[y])swap(x,y);
for(int i=20;i>=0;i--)
if (dep[pre[x][i]]>=dep[y])
{
ret=max(ret,f[sign][x][i]);
x=pre[x][i];
}
for(int i=20;i>=0;i--)
if (pre[x][i]!=pre[y][i])
{
ret=max(ret,f[sign][x][i]);
ret=max(ret,f[sign][y][i]);
x=pre[x][i];
y=pre[y][i];
}
if (x!=y)
{
ret=max(ret,f[sign][x][0]);
ret=max(ret,f[sign][y][0]);
}
return ret;
}
int getfather(int x)
{
if (father[x]==x)return x;
return father[x]=getfather(father[x]);
}
int main()
{
int T;
cin>>T;
while (T--)
{
scanf("%d%d",&n,&m);
rep(i,m)scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
sort(a+1,a+m+1);
rep(i,n)father[i]=i;
rep(i,n)g[i].clear();
vector<int> q; q.clear();
long long sum1=0; int cnt=0;
rep(i,m)
{
int t1=getfather(a[i].x);
int t2=getfather(a[i].y);
if (t1==t2){ q.pb(i); continue; }
g[a[i].x].pb(mp(a[i].y,a[i].w));
g[a[i].y].pb(mp(a[i].x,a[i].w));
father[t1]=t2;
sum1=sum1+a[i].w;
cnt++;
//cout<<a[i].x<<" "<<a[i].y<<" "<<a[i].w<<endl;
}
if (cnt!=n-1){ puts("-1 -1"); continue; }
for(int i=0;i<=20;i++)f[0][0][i]=f[1][0][i]=-2147483647;
long long sum2=2147483647ll*10000000ll;
dfs(1,0,0,1);
for(auto i:q)
{
int t=get(a[i].x,a[i].y,(a[i].w&1)^1);
//cout<<t<<endl;
if (t==-2147483647)continue;
sum2=min(sum2,sum1-t+a[i].w);
}
if (sum2==2147483647ll*10000000ll)sum2=-1;
if (sum1%2==1)swap(sum1,sum2);
printf("%lld %lld\n",sum1,sum2);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11976kb
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: 117ms
memory: 11928kb
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 3038805477 1262790434 1126848677 1263932600 1307926149 1153542856 1180186165 2248358640 2094849259 3776889482 3738936375 971832530 1058677117 3168165864 3402127725 956365412 1187873655 1395482806 1369731667 3456885934 3459440551 3943951826 3644735087 2479987500 2500480131 2909126794 26631...
result:
wrong answer 2nd numbers differ - expected: '3159441841', found: '3038805477'