QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#185800 | #6402. MEXimum Spanning Tree | UESTC_Guest_WiFi# | WA | 4ms | 7892kb | C++20 | 1.9kb | 2023-09-22 16:58:08 | 2023-09-22 16:58:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e3;
int n,m;
struct E{
int u,v,in;
};
vector<E> we[N+5];
int ew[N+5][N+5];
int f[N+5];
int fa[N+5],dep[N+5];
int find(int x){ return f[x]==x?x:f[x]=find(f[x]); }
bool merge(int u,int v){
u=find(u); v=find(v);
if(u==v) return 0;
f[u]=v;
return 1;
}
vector<int> e[N+5];
void dfs(int u,int tf){
dep[u]=dep[tf]+1;
fa[u]=tf;
for(auto v:e[u])
if(v!=tf) dfs(v,u);
}
void prep(int k){
for(int i=1;i<=n;i++){
e[i].clear();
dep[i]=0;
}
for(int i=0;i<k;i++)
for(auto j:we[i])
if(j.in){
e[j.u].push_back(j.v);
e[j.v].push_back(j.u);
}
for(int i=1;i<=n;i++)
if(!dep[i]) dfs(i,0);
}
vector<int> LCA(int u,int v){
vector<int> res;
while(u!=v){
if(dep[u]<dep[v]) swap(u,v);
res.push_back(ew[u][fa[u]]);
u=fa[u];
}
return res;
}
bool trye(int u,int v){
if(merge(u,v)) return 1;
int fu=find(u);
vector<int> possc=LCA(u,v);
bool ok=0;
for(auto i:possc){
for(auto &j:we[i]){
if(ok) j.in=0;
else if(!j.in&&(find(j.u)==fu||find(j.v)==fu)&&merge(j.u,j.v)){
j.in=1; ok=1;
}
}
if(ok) return 1;
}
return 0;
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1,u,v,w;i<=m;i++){
scanf("%d %d %d",&u,&v,&w);
we[w].push_back(E{u,v,0});
ew[u][v]=ew[v][u]=w;
}
for(int i=1;i<=n;i++) f[i]=i;
for(int res=0;res<=n;res++){
prep(res);
bool ok=0;
for(auto &i:we[res])
if(trye(i.u,i.v)){
ok=1; i.in=1; break;
}
if(!ok){
printf("%d\n",res);
break;
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3868kb
input:
4 4 1 2 0 2 3 1 1 3 1 3 4 2
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 4ms
memory: 7892kb
input:
1000 1000 647 790 6 91 461 435 90 72 74 403 81 240 893 925 395 817 345 136 88 71 821 831 962 53 164 270 298 14 550 317 99 580 81 26 477 488 977 474 861 413 483 167 872 675 17 819 327 449 594 242 68 381 983 319 867 582 358 869 225 669 274 352 392 40 388 998 246 477 44 508 979 286 483 776 71 580 438 6...
output:
502
result:
ok 1 number(s): "502"
Test #3:
score: -100
Wrong Answer
time: 3ms
memory: 7412kb
input:
900 1000 232 890 107 425 399 19 5 74 753 105 333 163 779 42 582 359 647 524 767 409 48 239 780 443 484 489 546 97 634 562 627 866 714 500 357 590 60 728 591 407 686 210 547 32 370 76 772 500 407 584 772 73 699 69 332 847 516 829 754 727 562 756 678 819 303 128 781 667 263 535 672 767 89 762 216 878 ...
output:
365
result:
wrong answer 1st numbers differ - expected: '801', found: '365'