QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#792492 | #6560. Broken Minimum Spanning Tree | lukamosiashvili# | WA | 2ms | 8944kb | C++17 | 3.3kb | 2024-11-29 10:48:56 | 2024-11-29 10:48:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
array <int, 4> v[3009];
int par[3009], in[3009], t, bo[3009];
vector <pair <int, int> > adj[3009];
int fnd(int a){
if(par[a] == a) return a; else return par[a] = fnd(par[a]);
}
void mrg(int a, int b){
a = fnd(a);
b = fnd(b);
if(a == b) return;
par[b] = a;
}
vector <int> st;
vector <pair <int, int> > ans;
int edge;
void dfs(int v){
bo[v] = 1;
for(auto to:adj[v]){
if(to.first == t){
//cout << "found\n";
st.push_back(to.second);
for(int j = 0; j < st.size(); j++){
if(in[st[j]]){
in[st[j]] = 0;
ans.push_back({edge, st[j]});
t = -1;
return;
}
}
return;
}
if(bo[to.first]) continue;
st.push_back(to.second);
dfs(to.first);
if(t == 1) return;
}
st.pop_back();
}
pair <int, int> PA[200009];
vector <int> ad[200009];
void DFS(int v, int par){
bo[v] = 1;
for(auto to:ad[v]){
if(to != par) DFS(v, to);
}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> v[i][2] >> v[i][3] >> v[i][0];
PA[i] = {v[i][2], v[i][3]};
if(i < n){
v[i][1] = -i;
}else{
v[i][1] = i;
}
}
sort(v + 1, v + m + 1);
for(int i = 1; i <= n; i++) par[i] = i;
for(int i = 1; i <= m; i++){
if(fnd(v[i][2]) != fnd(v[i][3])){
mrg(v[i][2], v[i][3]);
adj[v[i][2]].push_back({v[i][3], abs(v[i][1])});
adj[v[i][3]].push_back({v[i][2], abs(v[i][1])});
in[i] = 1;
}
}
vector <int> a, b;
for(int i = 1; i <= m; i++){
if(v[i][1] < 0 && in[i] == 0){
b.push_back(-v[i][1]);
}
if(in[i] == 1 && v[i][1] < 0) a.push_back(abs(v[i][1]));
}
//for(int i = 0; i < a.size(); i++) ans.push_back({b[i], a[i]});
for(int i = 0; i < b.size(); i++){
for(int j = 0; j <= n; j++) ad[j].clear();
for(int j = 0; j < a.size(); j++){
ad[PA[a[j]].first].push_back(PA[a[j]].second);
ad[PA[a[j]].second].push_back(PA[a[j]].first);
//cout << a[j] << " ";
}
//cout << endl;
//cout << b[i] << " ";
for(int j = i + 1; j < b.size(); j++){
ad[PA[b[j]].first].push_back(PA[b[j]].second);
ad[PA[b[j]].second].push_back(PA[b[j]].first);
//cout << b[j] << " ";
}
//cout << endl;
for(int j = 0; j <= n; j++) bo[j] = 0;
DFS(1, 0);
for(int j = 1; j <= m; j++){
int ind = abs(v[j][1]);
if(in[j] == 1 && v[j][1] > 0 && bo[PA[ind].first] != bo[PA[ind].second]){
v[j][1] = - v[j][1];
ans.push_back({b[i], ind});
a.push_back(ind);
break;
}
}
}
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++){
cout << ans[i].first << " " << ans[i].second << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8944kb
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: 2ms
memory: 8868kb
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:
1 4 7
result:
wrong answer not a spanning tree after swap 1