QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#355670 | #6560. Broken Minimum Spanning Tree | ec1117 | WA | 1ms | 3628kb | C++14 | 2.9kb | 2024-03-17 01:32:38 | 2024-03-17 01:32:39 |
Judging History
answer
#include <bits/stdc++.h>
#include <random>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
#define bk back()
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define For(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7;
const ld PI = acos((ld)-1);
mt19937 rng; // or mt19937_64
template<class T> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
const int MX = 3e3;
/**
* Description: Disjoint Set Union with path compression
* and union by size. Add edges and test connectivity.
* Use for Kruskal's or Boruvka's minimum spanning tree.
* Time: O(\alpha(N))
* Source: CSAcademy, KACTL
* Verification: USACO superbull
*/
struct DSU {
vi e; void init(int n) { e = vi(n,-1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size
x = get(x), y = get(y); if (x == y) return 0;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y]; e[y] = x; return 1;
}
};
/**template<class T> T kruskal(int n, vector<pair<T,pi>> ed) {
sort(all(ed));
T ans = 0; DSU D; D.init(n+1); // edges that unite are in MST
trav(a,ed) if (D.unite(a.s.f,a.s.s)) ans += a.f;
return ans;
}*/
vector<pair<pi,int>> adj[MX];
vector<pair<pi,pi>> edg;
vpi ans;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m;cin>>n>>m;
For(i,m) {
int u,v,w;cin>>u>>v>>w;
u--;v--;
adj[u].pb({{v,w},i});
adj[v].pb({{u,w},i});
edg.pb({{u,v},{w,i}});
}
For(i,n-1) {
int w = -1;
DSU dsu;
dsu.init(n);
trav(x,edg) {
if(x.s.s > i && x.s.s<n-1) {
dsu.unite(x.f.f,x.f.s);
}
if (x.s.s == i) {
assert(w==-1);
w = x.s.f;
}
}
int mn = w, mni=-1;
trav(x,edg) {
if (x.s.s>=n-1 && x.s.f<mn && !dsu.sameSet(x.f.f,x.f.s)) {
mn = x.s.f;
mni=x.s.s;
}
}
if (mn!=w) {
ans.pb({i+1, mni+1});
}
}
cout << ans.size() << '\n';
trav(x,ans) {
cout << x.f << " " << x.s << '\n';
}
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3628kb
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: 0ms
memory: 3588kb
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:
5 1 7 2 7 3 7 4 7 5 7
result:
wrong answer bad swap 2 7