QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#355774 | #6560. Broken Minimum Spanning Tree | kevinshan | WA | 1ms | 3644kb | C++17 | 2.1kb | 2024-03-17 07:51:55 | 2024-03-17 07:51:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second
const int MAXN = 2005;
vector<int> adj[MAXN];
int comp[MAXN];
void dfs(int x, int p) {
comp[x] = 1;
for(int i:adj[x]) {
if(i == p) continue;
dfs(i,x);
}
}
void populate(int x, int n) {
for(int i=0; i<n; i++) comp[i] = 0;
dfs(x,-1);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
int n, m;
cin>>n>>m;
set<int> active;
set<int> available;
int eds[m][2];
int wt[m];
for(int i=0; i<m; i++){
int u, v, w;
cin>>u>>v>>w;
u--; v--;
eds[i][0] = u;
eds[i][1] = v;
wt[i] = w;
if(i<n-1) active.insert(i);
else available.insert(i);
}
vector<pair<int, int>> ord;
for(int i=0; i<n-1; i++) ord.pb({i, wt[i]});
sort(all(ord), greater<pair<int, int>>());
vector<pair<int, int>> res;
for(auto p:ord) {
int i = p.f;
if(active.find(i) != active.end()) continue;
for(int e:active) {
if(e == i) continue;
adj[eds[e][0]].pb(eds[e][1]);
adj[eds[e][1]].pb(eds[e][0]);
}
populate(0,n);
for(int i=0; i<n; i++) adj[i].clear();
int best = wt[i];
int org = i;
for(int e:available) {
if(comp[eds[e][0]] == comp[eds[e][1]]) continue;
if(wt[e] < best) {
best = wt[e];
org = e;
}
}
if(org == i) continue;
active.erase(active.find(i));
active.insert(org);
res.pb({i, org});
}
for(int i=n-1; i<m; i++) {
if(available.find(i) != available.end()) {
available.erase(available.find(i));
}
}
cout<<res.size()<<"\n";
for(auto p:res){
cout<<p.f+1<<" "<<p.s+1<<"\n";
}
}
// traverse edges in reverse order
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3644kb
input:
4 4 1 2 10 2 3 3 3 4 1 1 4 4
output:
0
result:
FAIL participant's MST is better than jury!