QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#507354#7671. Metal Processing PlantZpairWA 1ms3852kbC++202.6kb2024-08-06 16:41:242024-08-06 16:41:24

Judging History

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

  • [2024-08-06 16:41:24]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3852kb
  • [2024-08-06 16:41:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=205;
int d[N][N],n;
struct node{
	int x,y,v;
	bool operator <(const node &ret)const{
		return v>ret.v;
	}
}t[N*N];
int m;
int f[N];
int find(int x){
	return x==f[x]?x:f[x]=find(f[x]);
}
vector<int> e[N];
int dep[N],fa[N],lca[N][N];
void dfs(int p){
	dep[p]=dep[fa[p]]+1;
	for(int t:e[p]){
		if(t==fa[p])continue;
		fa[t]=p,dfs(t);
	}
}
int LCA(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	while(dep[x]!=dep[y])x=fa[x];
	while(x!=y)x=fa[x],y=fa[y];
	return x;
}
int dis(int x,int y){
	return dep[x]+dep[y]-2*dep[lca[x][y]];
}
namespace ck{
	const int M=N+N;
	vector<int> e[M],g[M];
	void add(int x,int y){
		e[x].push_back(y);
		g[y].push_back(x);
	}
	bool vis[M];
	int q[M],tp;
	int bel[M],tot;
	void dfs1(int p){
		vis[p]=1;
		for(int t:e[p])
			if(!vis[t])
				dfs1(t);
		q[++tp]=p;
	}
	void dfs2(int p){
		bel[p]=tot;
		for(int t:g[p])
			if(!bel[t])
				dfs2(t);
	}
	bool check(){
		for(int i=1;i<=n+n;++i)
			if(!vis[i])dfs1(i);
		for(int i=n+n;i>=1;--i)
			if(!bel[q[i]])++tot,dfs2(q[i]);
		bool pd=1;
		for(int i=1;i<=n;++i)
			if(bel[i]==bel[i+n])
				pd=0;
		tp=tot=0;
		for(int i=1;i<=n+n;++i){
			e[i].clear();
			g[i].clear();
		}
		memset(vis,0,sizeof(vis));
		memset(q,0,sizeof(q));
		memset(bel,0,sizeof(bel));
		return pd;
	}
}
using ck::add;
using ck::check;
bool check(int dA,int dB){
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j){
			if(d[i][j]>dA){
				add(i,j+n);
				add(i+n,j);
				add(j,i+n);
				add(j+n,i);
			}
			else if(d[i][j]>dB){
				add(i+n,j);
				add(j+n,i);
			}
		}
	return check();
}
int mn=2e9+1;
void calc(int dA){
	int l=0,r=dA,mid=(l+r)>>1,ans=-1;
	while(l<=r){
		if(check(dA,mid))
			ans=mid,r=mid-1;
		else l=mid+1;
		mid=(l+r)>>1;
	}
	if(ans!=-1)
		mn=min(mn,dA+ans);
}//dA
int main(){
	cin>>n;
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j)
			scanf("%d",&d[i][j]);
	for(int i=1;i<=n;++i)
		for(int j=1;j<i;++j)
			d[i][j]=d[j][i];
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j)
			t[++m]={i,j,d[i][j]};
	sort(t+1,t+m+1);
	for(int i=1;i<=n;++i)f[i]=i;
	for(int i=1;i<=m;++i){
		int x=t[i].x,y=t[i].y;
		int fx=find(x),fy=find(y);
		if(fx!=fy){
			e[x].push_back(y);
			f[fx]=fy;
		}
	}
	dfs(1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			lca[i][j]=LCA(i,j);
	for(int i=1;i<=n;++i)f[i]=i;
	for(int i=1;i<=m;++i){
		int x=t[i].x,y=t[i].y,v=t[i].v;
		int fx=find(x),fy=find(y);
		if(fx!=fy)calc(v),f[fx]=fy;
		else if(dis(x,y)&1){
			calc(v);
			break;
		}
	}
	calc(0);
	cout<<mn;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3852kb

input:

5
4 5 0 2
1 3 7
2 0
4

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3676kb

input:

7
1 10 5 5 5 5
5 10 5 5 5
100 100 5 5
10 5 5
98 99
3

output:

15

result:

ok single line: '15'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3716kb

input:

1

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

2
1

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

2
1000000000

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

3
1000000000 1000000000
1000000000

output:

1000000000

result:

ok single line: '1000000000'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3764kb

input:

4
1000000000 1000000000 1000000000
1000000000 1000000000
1000000000

output:

1000000000

result:

ok single line: '1000000000'

Test #8:

score: -100
Wrong Answer
time: 0ms
memory: 3648kb

input:

3
78 97
24

output:

78

result:

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