답안 #1027995

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027995 2024-07-19T12:17:14 Z Dan4Life 도시들 (IOI15_towns) C++17
48 / 100
11 ms 1028 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
using ar2 = array<int,2>;
const int mxN = 115;
int n, vis[mxN];
int mem[mxN][mxN];
map<int,int> M;

int dis(int a, int b){
	if(a==b) return 0;
	if(mem[a][b]!=-1) return mem[a][b];
	if(mem[b][a]!=-1) return mem[b][a];
	return mem[a][b]=mem[b][a]=getDistance(a,b);
}

int p[mxN], sz[mxN];
int findSet(int i) { return p[i]==i?i:p[i]=findSet(p[i]); }
bool isSameSet(int i, int j) { return findSet(i)==findSet(j); }

void unionSet(int i, int j){
	int x = findSet(i), y = findSet(j);
	if(x==y) return;
	if(sz[x]<sz[y]) swap(x,y);
	p[y]=x; sz[x]+=sz[y];
}

int hubDistance(int N, int sub) {
	n = N; ar2 mx = {0,0};
	int a = 0, b = 0; M.clear();
	memset(mem,-1,sizeof(mem));
	for(int i = 0; i < n; i++) 
		mx=max(mx,{dis(a,i),i});
	a = mx[1]; mx = {0,0};
	for(int i = 0; i < n; i++)
		mx=max(mx,{dis(a,i),i});
	b = mx[1]; 
	int diameter = dis(a,b);
	int ans = diameter;

	for(int i = 0; i < n; i++){
		if(i==a or i==b) continue;
		int branch = (dis(a,i)+dis(b,i)-diameter)/2;
		int lpos = dis(a,i)-branch;
		int rpos = diameter-lpos;
		M[lpos]=max({M[lpos],branch,lpos,rpos});
	}
	for(auto u : M) ans=min(ans,u.second);
	if(sub<=2) return ans;
	int tot = 0;
	//for(auto u : M) if(u.second==ans)tot++;
	//if(tot>1) return ans;
	for(auto u : M){
		int lpos = u.first, mx_dis = u.second;
		int rpos = diameter-lpos;
		if(mx_dis!=ans) continue; 
		bool ok = 1;
		for(int i = 0; i < n; i++) 
			p[i]=i, sz[i]=1, vis[i]=0;
		for(int i = 0; i < n; i++){
			if(vis[findSet(i)]) continue;
			if(i!=a and i!=b and i!=0) continue;
			int i_u = dis(a,i)-lpos;
			if(dis(a,i)<dis(b,i)) i_u = dis(b,i)-rpos;
			
			for(int j = 0; j < n; j++){
				int u_j = dis(a,j)-lpos;
				if(dis(a,j)<dis(b,j)) u_j = dis(b,j)-rpos;
				if(i_u+u_j != dis(i,j)) unionSet(i,j);
			}
			vis[findSet(i)]=1;
		}
		int x = findSet(a), y = findSet(b), z = findSet(0);
		if(sub==4 and max({sz[x],sz[y],sz[z]})>n/2) return -ans;
		int tot = sz[x]+sz[y]+sz[z]*(z!=x and z!=y);
		int left = n-tot;
		if(sub==4 and left<=n/2) return ans;
		if(sub==4) return -ans;
		
		for(int i = 0; i < n; i++) vis[i]=0;
		vis[x]=vis[y]=vis[z]=1;
		for(int i = 0; i < n; i++){
			if(vis[findSet(i)]) continue;
			int i_u = dis(a,i)-lpos;
			if(dis(a,i)<dis(b,i)) i_u = dis(b,i)-rpos;
			for(int j = 0; j < n; j++){
				int u_j = dis(a,j)-lpos;
				if(dis(a,j)<dis(b,j)) u_j = dis(b,j)-rpos;
				if(i_u+u_j != dis(i,j)) unionSet(i,j);
			}
			vis[findSet(i)]=1;
		}
		for(int i = 0; i < n; i++)
			ok&=(sz[findSet(i)]<=n/2);
		if(ok) return ans;
	}
	return -ans;
}

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:75:7: warning: declaration of 'tot' shadows a previous local [-Wshadow]
   75 |   int tot = sz[x]+sz[y]+sz[z]*(z!=x and z!=y);
      |       ^~~
towns.cpp:50:6: note: shadowed declaration is here
   50 |  int tot = 0;
      |      ^~~
towns.cpp:50:6: warning: unused variable 'tot' [-Wunused-variable]
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 7 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 10 ms 348 KB Output is correct
5 Correct 10 ms 536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 348 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 11 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 348 KB Output is correct
2 Correct 11 ms 1028 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 10 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -