bzoj3332
题解:
首先只有存在的路有可能有值
然后在存储矩阵的同时对于本来就有边的情况直接存下来这条边的值
然后跑一次最大生成树
在最大生成树的同时就可以求出矩阵的信息。
代码:
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
using namespace std;
const int N=1e3+;
int t,n,m,sta,fin,mapy[N][N],mapy2[N][N],fa[N],son[N][N],cnt,ans;
struct node
{
int sta,fin,wor;
}roa[N*];
int read()
{
int x=;
char ch=getchar();
bool positive=;
for (;!isdigit(ch);ch=getchar())
if (ch=='-')positive=;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';
return positive?x:-x;
}
void addedge(int sta,int fin)
{
mapy[sta][fin]=read();
roa[++cnt].sta=sta;
roa[cnt].fin=fin;
roa[cnt].wor=mapy[sta][fin];
}
int fath(int u)
{
return fa[u]=(fa[u]==u?u:fath(fa[u]));
}
void check(int sta,int fin)
{
if(sta==fin&&mapy[sta][fin]!=)ans=;
if(sta>fin&&mapy[sta][fin]!=mapy[fin][sta])ans=;
}
void init()
{
n=read();m=read();cnt=;
clr(mapy);clr(mapy2);clr(fa);
clr(son);clr(roa);
for(int i=;i<=n;i++)fa[i]=son[i][++son[i][]]=i;
for(int i=;i<=m;i++)
{
sta=read();fin=read();
if(sta>fin)swap(sta,fin);
mapy[sta][fin]=;
}
ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(mapy[i][j])addedge(i,j);
else mapy[i][j]=read();
check(i,j);
}
}
bool cmp(node x,node y)
{
return x.wor>y.wor;
}
void unio(int u)
{
int fa1=fa[roa[u].sta],fa2=fa[roa[u].fin];
fa[fa1]=fa2;
for(int i=;i<=son[fa1][];i++)
for(int j=;j<=son[fa2][];j++)
mapy2[son[fa1][i]][son[fa2][j]]=
mapy2[son[fa2][j]][son[fa1][i]]=roa[u].wor;
for(int i=;i<=son[fa1][];i++)son[fa2][++son[fa2][]]=son[fa1][i];
}
int work(int cur)
{
printf("Case #%d: ",cur);
if(ans)return ans;
sort(roa+,roa+m+,cmp);
for(int i=;i<=m;i++)
{
if(fath(roa[i].sta)==fath(roa[i].fin))continue;
unio(i);
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(fath(i)!=fath(j)){if(mapy[i][j]!=-)ans=;}
else if(mapy2[i][j]!=mapy[i][j])ans=;
}
return ans;
}
int main()
{
t=read();
for (int i=;i<=t;i++)
{
init();
if(work(i))puts("No");
else puts("Yes");
}
return ;
}
bzoj3332的更多相关文章
- bzoj3332: 旧试题
		
这题就是最大生成树. 把两个点之间的期望建边排序. 把相同的期望一起做,那么在这个做之前,这些有着相同期望的点两两肯定不连,否则就输出No了. 相同的做完之后,再次for一遍check一下有没有两两之 ...
 
随机推荐
- C语言: 两个int变量相除,结果保留两位小数
			
#include<stdio.h> void main() { ,j=; float h; h=(*/)/; printf("%.2f",h); } 注:%f:不指定宽 ...
 - ubuntu下桌面假死处理方法(非重启)
			
一.背景 2018/05/22,就在这一天,进入ubuntu的桌面后随便点击任何位置均无法响应,此时又不想重启,遂出此文 二.解决方案 2.1 关掉Xorg进程 2.1.1按下ctrl+alt+F1进 ...
 - 快速排序|2018年蓝桥杯B组题解析第五题-fishers
			
标题:快速排序 以下代码可以从数组a[]中找出第k小的元素. 它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的. 请仔细阅读分析源码,填写划线部分缺失的内容. #include <s ...
 - TeeChart的坐标轴
			
TeeChart一共有六个坐标轴,一下是默认值 tChart1.Axes.Bottom.Visible = true;//横轴 tChart1.Axes.Left.Visible = true;//纵 ...
 - Java中线程出现Exception in thread "Thread-0" java.lang.IllegalMonitorStateException异常 解决方法
			
代码 package thread; public class TestChongNeng { public static void main(String[] args) { Thread t1 = ...
 - 4、CommonChunkPlugin提取公共js-提取多个
			
cnpm install css-loader --save-dev //css-loader 是将css打包进js cnpm install style-loader --save-dev ...
 - 【Coursera】Security Introduction -Eighth Week(1)
			
Security Introduction People With Bad Intent 今天,Bob 向 Alice 发送了一条 "Hello,Allice!" 的信息,他们希望 ...
 - IDEA启动时自动报Plugin Error错误
			
Plugin Error Problems found loading plugins: Plugin "JBoss Integration" was not loaded: re ...
 - 使用wsHttpBinding构建Message安全模式和UserName授权
			
http://www.cnblogs.com/artech/archive/2011/05/22/authentication_01.html https://www.cnblogs.com/Fran ...
 - Linux command line exercises for NGS data processing
			
by Umer Zeeshan Ijaz The purpose of this tutorial is to introduce students to the frequently used to ...