QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#355672#6560. Broken Minimum Spanning Treeec1117WA 1ms3628kbC++143.1kb2024-03-17 01:43:022024-03-17 01:43:04

Judging History

你现在查看的是最新测评结果

  • [2024-03-17 01:43:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3628kb
  • [2024-03-17 01:43:02]
  • 提交

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], inp;
vector<pair<pi,pi>> edg;
vpi ans;
set<pair<pi,int>> active;

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--;
		if(v<u)swap(u,v);
		adj[u].pb({{v,w},i});
		adj[v].pb({{u,w},i});
		edg.pb({{u,v},{w,i}});
		inp.pb({{u,v},w});
		if(i<n-1) {
			dbg(u,v,i);
			active.insert({{u,v},i});	
		}
	}
	For(i,n-1) {
		DSU dsu;
		dsu.init(n);
		trav(x,active) {
			if(x.s!=i) {
				dsu.unite(x.f.f,x.f.s);
			}
		}
		int mn = inp[i].s, mni=-1;
		trav(x,edg) {
			dbg(x.f.f, x.f.s, !active.count({x.f,x.s.s}), x.s.f);
			if (!active.count({x.f,x.s.s}) && !dsu.sameSet(x.f.f,x.f.s) && x.s.f < mn) {
				mn = x.s.f;
				mni = x.s.s;
			}
		}
		if (mn!=inp[i].s) {
			active.erase(inp[i]);
			active.insert(inp[mni]);
			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: 3576kb

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:

3
1 7
2 7
4 8

result:

wrong answer bad swap 2 7