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一下有没有两两之 ...
随机推荐
- static理解
static 修饰的变量称为类变量或全局变量或成员变量,在类被加载的时候成员变量即被初始化,与类关联,只要类存在,static变量就存在. 一个static变量单独划分一块存储空间,不与具体的对象绑定 ...
- 【前端】javascript+jQuery实现旋转木马效果轮播图slider
实现效果: 实现原理: 技术栈: javascript+jQuery+html+css 实现步骤: // 0. 获取元素 // 1. 鼠标放置到轮播图上,显示两侧的控制按钮,移开后隐藏 // 2. 为 ...
- Python3基础 tuple 创建空元组或者只有一个元素的元组 并 用乘法成倍扩充
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 用GDB调试Segmentation 段错误【转】
本文转载自:http://blog.csdn.net/learnhard/article/details/4879834 调试Linux程序的时候,出现Segmentation Fault是最郁闷的事 ...
- 64bit ubuntu如何使能安装32bit软件
答:使用一下命令即可: sudo dpkg --add-architecture i386
- POJ 2975 Nim(博弈)题解
题意:已知异或和为0为必败态,异或和不为0为必胜态,问你有几种方法把开局从当前状态转为必败态. 思路:也就是说,我们要选一堆石头,然后从这堆石头拿走一些使剩下的石碓异或和为0.那么只要剩下石堆的异或和 ...
- Springboot2.x 拦截器
一,单个拦截器,实现接口 HandlerInterceptor @Component public class MyInterceptor1 implements HandlerIntercepto ...
- FieldOffset
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.G ...
- Qt_QString.indesOf和mid测试
1.indexOf #define GID_PREFIX "dr_" QString str = "dr__awedr4"; int iIdx = str.in ...
- 【转】VC6在Win7下打开文件崩溃问题
http://www.cnblogs.com/Leon5/archive/2011/08/24/2152670.html 1.微软针对这个问题发布了一个补丁包.下载地址 2.下载之后是一个源码包,解压 ...