冬训练习5
来源:互联网 发布:魔兽世界mac版插件 编辑:程序博客网 时间:2024/06/05 21:53
A。欧拉回路
束。
3 31 21 32 33 21 22 30
10
欧拉回路
学习到了如何判断无向图的连通性
使用并查集
看看最后父节点有几个 只有一个的话就为连通的
并查集最后一般是循环 father[i]==i 来判断各种关系
无向图欧拉回路条件:
1.连通 2.度全为奇数
编程实现如下
#include<iostream>
#include<cstdio>
using namespace std;
const int maxV=1000;
const int maxE=maxV*maxV/2+7;
int degree[maxV+7];
int pre[maxV+7];
int E,V;
void init(){
for(int i=1;i<=maxV;i++) pre[i]=i;
}
int find(int x){
int temp;
for(temp=x;pre[temp]!=temp;temp=pre[temp]);
while(x!=temp){
int t=pre[x];
pre[x]=temp;
x=t;
}
return temp;
}
void mix(int x,int y){
int fx=find(x), fy=find(y);
if(fx!=fy) pre[fx]=fy;
}
bool isEuler(){
for(int i=1;i<=V;i++){
if(degree[i]&1) return false;
}
return true;
}
bool isConnct(){
int cnt=0;
for(int i=1;i<=V;i++)
if(pre[i]==i) cnt++;
if(cnt==1) return true;
return false;
}
int main(){
while(~scanf("%d",&V)&&V){
scanf("%d",&E);
memset(degree,0,sizeof(degree));
init();
for(int i=1;i<=E;i++){
int fr,to;
scanf("%d%d",&fr,&to);
degree[fr]++;
degree[to]++;
mix(fr,to);
}
// if(isConnct()) cout<<"Y"<<endl;
if(isConnct()&&isEuler()) cout<<1<<endl;
else cout<<0<<endl;
}
return 0;
}
B.还是畅通工程
当N为0时,输入结束,该用例不被处理。
31 2 11 3 22 3 441 2 11 3 41 4 12 3 32 4 23 4 50
35 Huge input, scanf is recommended.
Hint
方法思路是:
这道题目是求最小生成树
使用kruskal算法
每次取最小的边 ,判断这条边的两个节点是否已经取了,用并查集来维护
取了之后就加入并查集中
取权值最小的边,可以有多种方法,用优先队列,把边都放到优先队列中(超时)
用vector(超时) 用数组(156ms)就可以过
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int father[105];
int find_(int x){
if(x==father[x]) return x;
return father[x]=find_(father[x]);
}
int find(int x){
int temp;
for(temp=x;father[temp]!=temp;temp=father[temp]);
while(x!=temp){
father[x]=temp;
x=father[x];
}
return temp;
}
void merge(int u,int v){
int x=find(u);
int y=find(v);
if(x!=y)
father[x]=y;
}
class ss{
public:
int s,e,w;
friend bool operator < (const ss&a,const ss& o){
return a.w<o.w;
}
};
ss vec[100005];
int main(){
int n;
while(scanf("%d",&n)&&n){
int num=n*(n-1)/2;
int i;
for(i=0;i<num;i++){
scanf("%d%d%d",&vec[i].s,&vec[i].e,&vec[i].w);
}
for(i=1;i<=n;i++)
father[i]=i;
int ans=0;
sort(vec,vec+num);
/*while(!que.empty()){
ss tmp=que.top();
que.pop();
int s=tmp.s,e=tmp.e,w=tmp.w;
int x=find(s),y=find(e);
if(x!=y){
ans+=w;
father[x]=y;
}
}*/
for(i=0;i<num;i++){
ss tmp=vec[i];
int s=tmp.s,e=tmp.e,w=tmp.w;
int x=find(s),y=find(e);
if(x!=y){
ans+=w;
father[x]=y;
}
}
printf("%d\n",ans);
//cout<<ans<<endl;
}
return 0;
}
- 冬训练习5
- 2016寒假冬训练习#1
- 练习5
- 练习5
- 练习5
- 练习5
- 练习5
- 练习5
- 练习5
- 练习5
- 练习5
- 【SICP练习】5 练习1.9
- //2.11练习.练习5(1).p37
- 课堂练习54页动手练习5
- jQuery练习5——val()练习
- tensoflow练习5:自动编码器练习
- 练习5-5
- 练习5-4
- 自定义View日历
- iOS 常用的第三方库
- 世纪互联Mooncake版Power BI Embedded初试
- 数字金字塔
- sql学习日志
- 冬训练习5
- 欢迎使用CSDN-markdown编辑器
- Annaconda 增加删除镜像 channel
- HDU4768 Flyer
- Google VR技术大揭秘
- Matlab数据的可视化 -- 简易表面图
- 讲故事的和尚被打断了
- SSL 1376——完全背包
- 实现一个公用的自定义UITableviewCell