Submission #137508


Source Code Expand

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<string.h>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<complex>
#include<map>
#include<set>
#include<bitset>
#include<iostream>
#include<sstream>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < n; i++)
#define rrep(i,n) for(int i = 1; i <= n; i++)
#define drep(i,n) for(int i = n-1; i >= 0; i--)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y);
#define mins(x,y) x = min(x,y);
#define pb push_back
#define sz(x) (int)(x).size()
#define popcount __builtin_popcount
#define snuke srand((unsigned)clock()+(unsigned)time(NULL))
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef pair<int,P> Q;
typedef vector<int> vi;

const int MX = 2005, INF = 1000000000;
const ll LINF = 1000000000000000000ll;
const double eps = 1e-10;
const int dx[] = {-1,0,1,0}, dy[] = {0,-1,0,1}; //<^>v

int n, m, q;
int e[MX][MX];
int g[MX];

vector<Q> ge[MX];

// Union find
struct uf{
	vector<int> d;
	uf(){}
	uf(int sz):d(sz,-1){}
	void init(){
		rep(i,sz(d)) d[i] = -1;
	}
	int root(int x){
		if(d[x] < 0) return x;
		return d[x] = root(d[x]);
	}
	void unite(int x, int y){
		x = root(x); y = root(y);
		if(x == y) return;
		if(d[x] > d[y]) swap(x,y);
		if(d[x] == d[y]) d[x]--;
		d[y] = x;
	}
};
//

int main(){
	scanf("%d%d",&n,&m);
	int a, b, c;
	rep(i,m){
		scanf("%d%d%d",&a,&b,&c);
		e[a][b] = e[b][a] = c;
	}
	rep(i,n) g[i] = i;
	uf t(n+5);
	
	scanf("%d",&q);
	rep(qi,q){
		scanf("%d%d",&a,&b);
		a = g[a];
		b = g[b];
		vi x, y;
		
		rep(i,n) if(g[i] == a) x.pb(i);
		rep(i,n) if(g[i] == b) y.pb(i);
		
		vector<Q> p;
		rep(i,sz(x))rep(j,sz(y)){
			if(e[x[i]][y[j]]) p.pb(Q(e[x[i]][y[j]],P(x[i],y[j])));
		}
		rep(i,sz(ge[a])) p.pb(ge[a][i]);
		rep(i,sz(ge[b])) p.pb(ge[b][i]);
		t.init();
		ge[a].clear();
		ge[b].clear();
		sort(rng(p));
		
		int cnt = sz(x)+sz(y)-1;
		int ans = 0;
		rep(i,sz(p)){
			c = p[i].fi;
			int v = p[i].se.fi;
			int u = p[i].se.se;
			if(t.root(v) != t.root(u)){
				ans += c;
				t.unite(v,u);
				ge[a].pb(p[i]);
				--cnt;
			}
		}
		rep(i,n) if(g[i] == b) g[i] = a;
		if(cnt){
			puts("IMPOSSIBLE");
		} else {
			printf("%d\n",ans);
		}
		//cout << sz(x) << endl;
		//cout << sz(y) << endl;
	}
	
	return 0;
}




Submission Info

Submission Time
Task F - 魔法の糸
User snuke
Language C++ (G++ 4.6.4)
Score 200
Code Size 2604 Byte
Status AC
Exec Time 480 ms
Memory 23972 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:71:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:74:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:80:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:82:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]

Judge Result

Set Name All
Score / Max Score 200 / 200
Status
AC × 31
Set Name Test Cases
All 00-sample1, 00-sample2, 01-random-small-tree01, 01-random-small-tree02, 02-random-small-sparse01, 02-random-small-sparse02, 02-random-small-sparse03, 03-random-small-dense01, 03-random-small-dense02, 03-random-small-dense03, 03-random-small-dense04, 03-random-small-dense05, 11-random-large-tree01, 11-random-large-tree02, 11-random-large-tree03, 12-random-large-sparse01, 12-random-large-sparse02, 12-random-large-sparse03, 13-random-large-denseA01, 13-random-large-denseA02, 13-random-large-denseA03, 13-random-large-denseA04, 14-random-large-denseB01, 14-random-large-denseB02, 14-random-large-denseB03, 14-random-large-denseB04, 15-random-large-denseC01, 15-random-large-denseC02, 20-kill-naive, 21-kill-loop, 23-min
Case Name Status Exec Time Memory
00-sample1 AC 21 ms 916 KB
00-sample2 AC 26 ms 796 KB
01-random-small-tree01 AC 22 ms 1308 KB
01-random-small-tree02 AC 22 ms 1196 KB
02-random-small-sparse01 AC 22 ms 1308 KB
02-random-small-sparse02 AC 24 ms 1348 KB
02-random-small-sparse03 AC 22 ms 1424 KB
03-random-small-dense01 AC 21 ms 1424 KB
03-random-small-dense02 AC 23 ms 1308 KB
03-random-small-dense03 AC 23 ms 1428 KB
03-random-small-dense04 AC 28 ms 1324 KB
03-random-small-dense05 AC 23 ms 1432 KB
11-random-large-tree01 AC 199 ms 17836 KB
11-random-large-tree02 AC 198 ms 17188 KB
11-random-large-tree03 AC 194 ms 17440 KB
12-random-large-sparse01 AC 275 ms 23720 KB
12-random-large-sparse02 AC 275 ms 23592 KB
12-random-large-sparse03 AC 275 ms 23588 KB
13-random-large-denseA01 AC 341 ms 23592 KB
13-random-large-denseA02 AC 336 ms 23164 KB
13-random-large-denseA03 AC 342 ms 23828 KB
13-random-large-denseA04 AC 340 ms 23664 KB
14-random-large-denseB01 AC 359 ms 23304 KB
14-random-large-denseB02 AC 350 ms 23460 KB
14-random-large-denseB03 AC 359 ms 23556 KB
14-random-large-denseB04 AC 357 ms 23972 KB
15-random-large-denseC01 AC 359 ms 23168 KB
15-random-large-denseC02 AC 354 ms 23816 KB
20-kill-naive AC 480 ms 10548 KB
21-kill-loop AC 446 ms 12072 KB
23-min AC 22 ms 844 KB