QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#627009#4272. Phone Planschenxinyang2006#0 2ms12036kbC++203.4kb2024-10-10 14:27:322024-10-10 14:27:36

Judging History

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

  • [2024-10-10 14:27:36]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:12036kb
  • [2024-10-10 14:27:32]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}


/*ll power(ll p,int k = mod - 2){
	ll ans = 1;
	while(k){
		if(k % 2 == 1) ans = ans * p % mod;
		p = p * p % mod;
		k /= 2;	
	}
	return ans;
}*/
int n,m1,m2,_m1,_m2;
ll m;

struct eg{
	int u,v,w;
}E[2][200005],Q[2][200005];

bool cmp(eg p,eg q){
	return p.w < q.w;
}

ll result;
int fa[200005],col[200005],occ[200005],buc[2][200005];
vector <int> S[200005];
vector <pii> P[200005];
int fnd(int u){
	if(fa[u] == u) return u;
	return fa[u] = fnd(fa[u]);
}

void mrg(int u,int v){
//	printf("mrg %d %d\n",u,v);
	for(int p:S[u]) buc[0][col[p]]++;
	for(int p:S[v]) buc[1][col[p]]++;
/*	printf("Set %d:",u);
	for(int p:S[u]) printf("(%d,%d) ",p,col[p]);
	printf("\n");
	printf("Set %d:",v);
	for(int p:S[v]) printf("(%d,%d) ",p,col[p]);
	printf("\n");*/
	result += 1ll * SZ(S[u]) * SZ(S[v]);
	rep(c,1,n){
		result -= 1ll * buc[0][c] * buc[1][c];
//		printf("col %d [0]%d [1]%d\n",c,buc[0][c],buc[1][c]);
		buc[0][c] = buc[1][c] = 0; 
	}
	if(SZ(S[u]) < SZ(S[v])) swap(S[u],S[v]);
	for(int p:S[v]){
		S[u].eb(p);
		fa[p] = u;
	}
}

void smrg(vector <pii> &info,int u,int v){
	u = fnd(u);v = fnd(v);
	if(SZ(S[u]) < SZ(S[v])) swap(u,v);
	for(int p:S[v]){
		S[u].eb(p);
		fa[p] = u;
		info.eb(p,v);
	}
}

void fix(int &u){
	u = fa[u];
}

void chg(int u,int c){
//	assert(0);
	int fu = fa[u];
	result--;
	for(int p:S[fu]){
		if(col[p] == col[u]) result++;
		else if(col[p] == c) result--;
	}
	col[u] = c;

	result += occ[col[u]] - 1;
	result -= occ[c];
	occ[col[u]]--;
	occ[c]++;
}

int main(){
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	scanf("%d%d%d%lld",&n,&m1,&m2,&m);
	rep(i,1,m1) scanf("%d%d%d",&E[0][i].u,&E[0][i].v,&E[0][i].w);
	rep(i,1,m2) scanf("%d%d%d",&E[1][i].u,&E[1][i].v,&E[1][i].w);
	sort(E[0] + 1,E[0] + m1 + 1,cmp);
	sort(E[1] + 1,E[1] + m2 + 1,cmp);
	rep(u,1,n){
		fa[u] = u;
		S[u].eb(u);
	}
	rep(i,1,m1){
		fix(E[0][i].u);fix(E[0][i].v);
		if(E[0][i].u != E[0][i].v){
			Q[0][++_m1] = E[0][i];
			smrg(P[i],E[0][i].u,E[0][i].v);
		}
	}
	rep(u,1,n){
		col[u] = fa[u];
		result += occ[col[u]];
		occ[col[u]]++;
		fa[u] = u;
		S[u].clear();
		S[u].eb(u);
	}	
//	rep(u,1,n) printf("%d ",col[u]);
//	printf("\nresult=%lld\n",result);

	m1 = _m1;
	rep(i,1,m1) E[0][i] = Q[0][i];
	int j = m1,answer = inf + inf;
	rep(i,1,m2){
		fix(E[1][i].u);fix(E[1][i].v);
		if(E[1][i].u == E[1][i].v) continue;
		mrg(E[1][i].u,E[1][i].v);
//		printf("i=%d result %lld\n",i,result);
		while(result >= m && j >= 0){
			chkmin(answer,E[1][i].w + E[0][j].w);
			for(pii I:P[j]) chg(I.first,I.second);
			j--;
		}
	}
	if(answer == inf + inf) printf("-1\n");
	else printf("%d\n",answer);
	return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 6
Accepted
time: 0ms
memory: 12036kb

input:

6 4 4 9
1 2 1
2 3 2
1 4 3
3 4 4
5 6 40
1 5 30
2 6 20
3 6 10

output:

33

result:

ok single line: '33'

Test #2:

score: 0
Wrong Answer
time: 1ms
memory: 5764kb

input:

1 0 0 0

output:

-1

result:

wrong answer 1st lines differ - expected: '0', found: '-1'

Subtask #2:

score: 0
Time Limit Exceeded

Test #53:

score: 0
Time Limit Exceeded

input:

200000 100000 100000 7628995
23677 113459 839167193
165893 15365 669621527
26287 109671 214795757
156871 136723 522277985
9957 100463 806718116
104783 161385 156110975
184577 92225 545172629
57373 130083 980035338
167231 49597 919886451
115601 24325 717233004
99413 141311 737488449
83437 62759 76873...

output:


result:


Subtask #3:

score: 0
Wrong Answer

Test #103:

score: 0
Wrong Answer
time: 2ms
memory: 7952kb

input:

1 0 0 0

output:

-1

result:

wrong answer 1st lines differ - expected: '0', found: '-1'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%