[机房考试] 图论
来源:互联网 发布:shell编程如何保存 编辑:程序博客网 时间:2024/06/05 14:43
Problem 1. Color
Input file: color.in
Output file: color.out
Time limit: 2 second
CKY 国有n 个省,现在CKY 想制作一份地图,地图上相邻的省份需要颜色不一样,但CKY 国只能制作两
种颜料,请问CKY 能如愿制作地图么?
题目保证没有自环
Input
第1 行,1 个整数T,表示数据组数。接下来T 组数据,对于每组数据:
第1 行,2 个整数n;m,表示省的数量和省之间相邻关系的数量。
接下来m 行,每行2 个整数u; v,表示u; v 相邻。省份用1; 2; : : : ; n 编号。
Output
对于每组数据,输出YES 或NO。
Sample
color.in color.out
2
3 3
1 3
3 2
2 1
4 4
1 2
2 3
3 4
4 1
NO
YES
Note
• 对于30% 的数据,1 n 103;
Problem 3. teacher
Input file: teacher.in
Output file: teacher.out
Time limit: 1 second
成都七中高新校区举行老师春晚啦。
每一位老师上台表演节目,都会增加整场晚会的精彩程度。但由于请老师表演,学校需要给上台表演的老师
辛苦费,而学校经费有限,并不能保证请到所有老师上台。
在一个教研组的老师关系都比较好,所以往往会有起哄现象。如果该教研组只有一个老师上台,那么该老师
就愿意上台,如果同一教研组有多个老师表演,那么该教研组的所有老师都必须上台表演。
现在给出m 对老师(一对老师= 两个老师),对于第i 对老师,我们确定在同一教研组。
现在,学校给出了辛苦费的预算,请求出在不超过预算的情况下的最大精彩程度。
Input
第1 行,3 个整数n; m;w,表示老师人数,前文提到的老师在同一教研组的关系对数和学校的预算
接下来一行,有n 个整数wi,表示每位老师上台的辛苦费。
接下来一行,有n 个整数bi,表示每位老师上台增加的精彩程度。
接下来m 行,每行2 个整数u; v,表示老师u 和老师v 在同一教研组, 保证u; v 不相等
Output
输出最大的精彩程度
Sample
teacher.in teacher.out
3 1 5
3 2 5
2 4 2
1 2
6
teacher.in teacher.out
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
7
Note
• 对于30% 的数据,1 n 10 ,1 m 5,1 w 100
Input file: color.in
Output file: color.out
Time limit: 2 second
CKY 国有n 个省,现在CKY 想制作一份地图,地图上相邻的省份需要颜色不一样,但CKY 国只能制作两
种颜料,请问CKY 能如愿制作地图么?
题目保证没有自环
Input
第1 行,1 个整数T,表示数据组数。接下来T 组数据,对于每组数据:
第1 行,2 个整数n;m,表示省的数量和省之间相邻关系的数量。
接下来m 行,每行2 个整数u; v,表示u; v 相邻。省份用1; 2; : : : ; n 编号。
Output
对于每组数据,输出YES 或NO。
Sample
color.in color.out
2
3 3
1 3
3 2
2 1
4 4
1 2
2 3
3 4
4 1
NO
YES
Note
• 对于30% 的数据,1 n 103;
• 对于100% 的数据,1 T 5,1 n 105,1 m 2:5 105。
AC代码
#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <iostream>#define re return#define clr(a,b) memset(a,b,s(a))#define s(a) sizeof(a) #define up(i,m,n) for(int i=m;i<=n;i++)using namespace std;const int MAXN=2000005,MAXM=2050005;int m,n,head[MAXN];struct SKY{int pre,to;SKY( ){pre=to=0;}}g[MAXM];int b[MAXN];int ne;template <class T>inline int read(T &x){x = 0;T flag = 1;char ch = (char)getchar();while(ch<'0' || ch>'9'){if(ch == '-') flag = -1;ch = (char)getchar();}while(ch>='0' && ch<='9'){x = (x<<1) + (x<<3) + ch - '0';ch = (char)getchar();}x *= flag;return 1;}inline void add(int x,int y){ne++;g[ne].to=y;g[ne].pre=head[x];head[x]=ne;}inline int dfs(int x){for (int j=head[x];j!=0;j=g[j].pre){if (b[g[j].to]!=-1){if(b[g[j].to]==b[x])return 0;}else{b[g[j].to]=3-b[x];if(!dfs(g[j].to)) return 0;}}return 1;}int init(){clr(g,0);clr(head,0);memset(b,-1,s(b));read(n);read(m);ne=0;up(i,1,m){int x,y;read(x);read(y);add(x,y);add(y,x);}up(i,1,n) if(b[i]<0) { b[i]=1; if(!dfs(i)) return 0; }return 1;}int main(){freopen("color.in","r",stdin);freopen("color.out","w",stdout);int T;read(T);while(T--)printf("%s\n",init()==1?"YES":"NO");return 0;}
Problem 2. Book
Input file: book.in
Output file: book.out
Time limit: 1 second
CKY 国虽然魔法强大,但好像其他科技不是很发达。在CKY 国传播书本是一件很累的事情,因为CKY 国
没有印刷机,只能靠人们手抄。假如一个人写了一本书,想出版,就首先要让所在省的抄写神牛抄写,然后
运输到其他省,让其他省的抄写神牛继续抄写。jyb 最近写了一本书,现在jyb 想知道他的书传播到全国需要
多长时间。
Input
第1 行,2 个整数n; m; st,表示省份数、道路数和jyb 所在的省份。省份用1; 2; : : : ; n 编号。
第2 行,n 个整数ai,表示每个省的抄写神牛抄写本书需要花费的时间。
接下来m 行,每行3 个整数u; v;w,表示省u; v 之间一条时间花费为w 的双向边。
Output
输出传播到全国需要多长时间。若不能传播到全国,则输出-1.
Sample
book.in book.out
4 4 1
80 10 90 10
1 2 80
1 3 10
2 4 50
3 4 50
230
Note
• 对于50% 的数据,1 n 103;
• 对于100% 的数据,1 n 5000,1 st n,1 m 105,1 ai;w 107。
#include <ctime>#include <cmath>#include <cstdio>#include <string>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define re return#define clr(a,b) memset(a,b,s(a))#define s(a) sizeof(a) #define ioclear() ios::sync_with_stdio(false)#define up(i,m,n) for(int i=m;i<=n;i++)#define down(i,m,n) for(int i=n;i>=m;i--)using namespace std;int const MAXN=100005,MAXM=100005;int m,n,st,qu[MAXN*2+1],a[MAXN],head[MAXM];int dis[MAXN];bool flag[MAXN];struct star{int to,pre,w;star(){to=pre=w=0;}}g[MAXM*2];int ne;inline void add(int u,int v,int dis){ne++;g[ne].to=v;g[ne].pre=head[u];g[ne].w=dis;head[u]=ne;}template <class T>inline int read(T &x){x = 0;T flag = 1;char ch = (char)getchar();while(ch<'0' || ch>'9'){if(ch == '-') flag = -1;ch = (char)getchar();}while(ch>='0' && ch<='9'){x = (x<<1) + (x<<3) + ch - '0';ch = (char)getchar();}x *= flag;return 1;}int INF;int SPFA(int x){ int h=0,t=1; qu[1]=x; dis[x]=0; flag[h]=true;//*** do { h=h%MAXN+1; int u=qu[h]; flag[u]=false;//***** for (int i=head[u];i!=0;i=g[i].pre) if (dis[g[i].to]>dis[u]+g[i].w+a[u]) { dis[g[i].to]=dis[u]+g[i].w+a[u]; if(!flag[g[i].to]) { flag[g[i].to]=true; t=t%MAXN+1; qu[t]=g[i].to; } } }while(h!=t); int MAX=-1; up(j,1,n) if(dis[j]==INF) re -1; else MAX=max(MAX,dis[j]+a[j]); return MAX;}int init(){clr(flag,0);clr(dis,60);read(n);read(m);read(st); up(i,1,n) read(a[i]);up(i,1,m) { int x,y; read(x);read(y); int temp; read(temp); add(x,y,temp); add(y,x,temp); }}int main(){freopen("book.in","r",stdin);freopen("book.out","w",stdout);init();INF=dis[1];printf("%d\n",SPFA(st));re 0;}
Problem 3. teacher
Input file: teacher.in
Output file: teacher.out
Time limit: 1 second
成都七中高新校区举行老师春晚啦。
每一位老师上台表演节目,都会增加整场晚会的精彩程度。但由于请老师表演,学校需要给上台表演的老师
辛苦费,而学校经费有限,并不能保证请到所有老师上台。
在一个教研组的老师关系都比较好,所以往往会有起哄现象。如果该教研组只有一个老师上台,那么该老师
就愿意上台,如果同一教研组有多个老师表演,那么该教研组的所有老师都必须上台表演。
现在给出m 对老师(一对老师= 两个老师),对于第i 对老师,我们确定在同一教研组。
现在,学校给出了辛苦费的预算,请求出在不超过预算的情况下的最大精彩程度。
Input
第1 行,3 个整数n; m;w,表示老师人数,前文提到的老师在同一教研组的关系对数和学校的预算
接下来一行,有n 个整数wi,表示每位老师上台的辛苦费。
接下来一行,有n 个整数bi,表示每位老师上台增加的精彩程度。
接下来m 行,每行2 个整数u; v,表示老师u 和老师v 在同一教研组, 保证u; v 不相等
Output
输出最大的精彩程度
Sample
teacher.in teacher.out
3 1 5
3 2 5
2 4 2
1 2
6
teacher.in teacher.out
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
7
Note
• 对于30% 的数据,1 n 10 ,1 m 5,1 w 100
• 对于100% 的数据,1 n 1000 ,1 m min(n(n
#include <ctime>#include <cmath>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <cstdlib>#include <iostream>#include <algorithm>#define re return#define clr(a,b) memset(a,b,s(a))#define s(a) sizeof(a) #define up(i,m,n) for(int i=m;i<=n;i++)using namespace std;const int MAXN=1005,MAXM=100005,MAXW=1005;int fa[MAXN],w[MAXN],b[MAXN],flag[MAXN],pos[MAXN],f[MAXN][MAXW];int ww[MAXN],bb[MAXN];vector<int > g[MAXN];int N,M,W,C=0;template <class T>inline int read(T &x){x = 0;T flag = 1;char ch = (char)getchar();while(ch<'0' || ch>'9'){if(ch == '-') flag = -1;ch = (char)getchar();}while(ch>='0' && ch<='9'){x = (x<<1) + (x<<3) + ch - '0';ch = (char)getchar();}x *= flag;return 1;}int getfa(int x){return x==fa[x]?x:fa[x]=getfa(fa[x]);}void Union(int x,int y){fa[getfa(x)]=getfa(y);//指向最远; }void init(){clr(f,0);read(N);read(M);read(W);up(i,1,N) fa[i]=i,read(w[i]); up(i,1,N) read(b[i]);up(i,1,M){int x,y;read(x);read(y);Union(x,y);}}int dp(){up(i,1,C){for(int j = 0; j <= W; j++)if(j >= ww[i])f[i][j] = max(f[i-1][j],f[i-1][j-ww[i]]+bb[i]);elsef[i][j] = f[i-1][j];if(g[i].size() == 1)continue;elseup(k,0,g[i].size()-1)up(j,0,W)if(j >= w[g[i][k]])f[i][j] = max(f[i][j],f[i-1][j-w[g[i][k]]]+b[g[i][k]]);}re f[C][W];}int solve(){up(i,1,N){fa[i]=getfa(i);if (fa[i]==i){C++;pos[i]=C;g[C].push_back(i);bb[C]+=b[i];ww[C]+=w[i];}}up(i,1,N){fa[i]=getfa(i);if(fa[i]!= i){g[pos[fa[i]]].push_back(i);bb[pos[fa[i]]] += b[i];ww[pos[fa[i]]] += w[i];} } re dp();}int main(){freopen("teacher.in","r",stdin);freopen("teacher.out","w",stdout);init();cout<<solve()<<endl;;}
0 0
- [机房考试] 图论
- [机房考试] 图论 题解
- 机房
- 考试
- 考试!
- 考试
- 考试
- 考试
- 考试
- 考试
- 考试
- 考试
- 考试
- 考试
- 考试
- 考试!
- 考试
- 考试
- 【C++】学习笔记二十七——读取数字的循环
- poj_3411 Paid Roads(dfs技巧)
- IOC前世今生
- PAT. basic level. 1002
- 如何使用Alfred提升操作效率(上)
- [机房考试] 图论
- 苏嵌33 17.01.18
- LINUX ECHO 颜色
- 嵌入式数据结构
- 【POJ 2236 Wireless Network】 + 并查集
- 图像拼接(四):双摄像头实时视频拼接(平移模型)
- LightningChart Ultimate v.7.2 版本功能特性对比
- 超级父类:Object类
- python快速入门(一)