QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#124738 | #6560. Broken Minimum Spanning Tree | Sommohito# | WA | 1ms | 3604kb | C++20 | 3.2kb | 2023-07-15 15:05:30 | 2023-07-15 15:05:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#ifdef APURBA
#include "DEBUG_TEMPLATE.h"
#else
#define HERE
#define debug(args...)
#endif
#define ALL(x) x.begin(),x.end()
typedef pair<int,int> pii;
const int N = 3003;
struct DSU
{
vector<int>parent;
vector<int>rnk;
int n;
DSU(int nn)
{
n=nn;
parent.resize(n);
rnk.resize(n);
for(int i=0; i<n; i++)
{
make_set(i);
}
}
void make_set(int x)
{
rnk[x] = 0;
parent[x] = x;
}
int find_set(int x)
{
if(parent[x]!=x)
{
parent[x] = find_set(parent[x]);
}
return parent[x];
}
void Union(int u,int v)
{
int p= find_set(u),q=find_set(v);
if(p==q)
return;
if( rnk[p] > rnk[q])
swap(p,q);
parent[p]= q;
if(rnk[p]==rnk[q])
{
rnk[q] +=1;
}
}
} dsu(N);
array<int,3> e[N];
map<int,int> cnt;
map<int,int> now;
map<int,vector<int> > eg;
set<pii> g[N];
bool ase[N];
vector<pii> ans;
int ret;
bool gt(int u,int p,int t)
{
if(u==t) return 1;
for(auto [v,i]: g[u])
if(v!=p)
{
if(gt(v,u,t))
{
auto &[uu,vv,ww] = e[i];
debug(i,now[ww],cnt[ww]);
if(now[ww]>cnt[ww])
{
ret=max(ret,i);
}
debug(ret);
return 1;
}
}
return 0;
}
void rep(int i)
{
auto &[u,v,w] = e[i];
ret=-1;
debug(u,v,i,ret);
gt(u,-1,v);
debug(ret);
assert(ret!=-1);
auto &[uu,vv,ww] = e[ret];
assert(ase[ret]==1);
assert(ase[i]==0);
g[uu].erase({vv,ret});
g[vv].erase({uu,ret});
g[u].insert({v,i});
g[v].insert({u,i});
now[ww]--;
now[w]++;
ase[ret]=0;
ase[i]=1;
ans.push_back({ret,i});
}
void TEST_CASES()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
auto &[u,v,w] = e[i];
cin>>u>>v>>w;
if(i<=n-1)
{
g[u].insert({v,i});
g[v].insert({u,i});
now[w]++;
ase[i]=1;
}
eg[w].push_back(i);
}
vector<pii> es(m);
for(int i=0;i<n;i++)
{
auto &[u,v,w] = e[i+1];
es[i]={w,i+1};
}
sort(ALL(es));
for(auto [_,i]: es)
{
auto &[u,v,w] = e[i];
if(dsu.find_set(u)!=dsu.find_set(v))
{
dsu.Union(u,v);
cnt[w]++;
}
}
for(auto [w,x]: cnt)
{
int &y = now[w];
assert(y<=x);
if(y == x) continue;
for(int i: eg[w])
{
if(x == y) break;
if(ase[i]) continue;
rep(i);
}
}
cout<<ans.size()<<"\n";
for(auto [a,b]: ans) cout<<a<<" "<<b<<"\n";
}
/*
*/
int32_t main()
{
#ifndef APURBA
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
//freopen("input.txt","r",stdin);
//freopen("out1.txt","w",stdout);
int t=1;
//cin>>t;
while(t--)
{
TEST_CASES();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
4 4 1 2 10 2 3 3 3 4 1 1 4 4
output:
1 1 4
result:
ok correct!
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3604kb
input:
6 8 1 2 10 2 3 10 3 4 10 4 5 10 5 6 10 6 1 10 1 3 1 4 6 1
output:
0
result:
FAIL participant's MST is better than jury!