先上题目:

Graphs

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus

Problem Description

给出N个点,M条边,问是否存在一个连通子图,子图由原图删掉一些点和边(不删亦可),且叶子数>=4(即度为1的点)

Input

多组数据,每组数据N,M(0 <= N <= 10000,0 <= M <= 20000)

接下来M行每行给出一条边的两个端点x,y (1 <= x ,y <= N),保证无重边,无自环

Output

对于每组数据,输出YES,如果你能找到这样的子图,否则输出NO

Sample Input

  1. 2 1
  2. 1 2
  3. 5 4
  4. 1 2
  5. 1 3
  6. 1 4
  7. 1 5

Sample Output

  1. NO
  2. YES
  3.  
  4.   根据题意,我们可以将点分成3种:①度小于3的点,②度等于3的点,③度大于等于4的点。
      对于①,我们可以直接跳过,因为这种点无论是单个还是组合都无法产生符合要求的子图。对于②,如果有两个度为三的点连载一起并且重合的点小于等于1个的话就有可能产生符合要求的子图。对于③,一个点就可以引出符合要求的子图。
      所以我们可以先判断是否有③的点,如果有就直接输出YES,否则判断所有度为③的点是否有符合要求的,如果有就直接输出YES,否则就不存在题目要求的子图。
  5.  
  6. 上代码:
  1. #include <cstdio>
  2. #include <cstring>
  3. #define MAX 100002
  4. using namespace std;
  5.  
  6. int c[MAX][],p[MAX],d[MAX],a[MAX],co,n,m;
  7.  
  8. int findset(int u){
  9. return u==p[u] ? u : p[u]=findset(p[u]);
  10. }
  11.  
  12. bool check_(int x,int y){
  13. int ans=;
  14. for(int i=;i<;i++){
  15. if(c[x][i]==y) ans++;
  16. else{
  17. for(int j=;j<;j++){
  18. if(c[x][i]==c[y][j]) ans++;
  19. }
  20. }
  21. }
  22. return ans<=;
  23. }
  24.  
  25. bool check(){
  26. co=;
  27. for(int i=;i<n;i++){
  28. if(d[i]>=) return ;
  29. else if(d[i]==) a[co++]=i;
  30. }
  31. for(int i=;i<co;i++){
  32. for(int j=i+;j<co;j++){
  33. if(findset(a[i])==findset(a[j]) && check_(a[i],a[j])) return ;
  34. }
  35. }
  36. return ;
  37. }
  38.  
  39. int main()
  40. {
  41. int u,v;
  42. //freopen("data.txt","r",stdin);
  43. while(scanf("%d %d",&n,&m)!=EOF){
  44. for(int i=;i<=n;i++) p[i]=i;
  45. memset(d,,sizeof(d));
  46. for(int i=;i<m;i++){
  47. scanf("%d %d",&u,&v);
  48. if(d[u]<) c[u][d[u]]=v;
  49. if(d[v]<) c[v][d[v]]=u;
  50. d[u]++; d[v]++;
  51. u = findset(u);
  52. v = findset(v);
  53. if(u!=v) p[v]=p[u];
  54. }
  55. if(check()) printf("YES\n");
  56. else printf("NO\n");
  57. }
  58. return ;
  59. }

Graphs

ACDream - Graphs的更多相关文章

  1. tunning-Instruments and Flame Graphs

    On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...

  2. Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图

    https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...

  3. ACdream 1214---矩阵连乘

    ACdream 1214---矩阵连乘 Problem Description You might have noticed that there is the new fashion among r ...

  4. acdream.LCM Challenge(数学推导)

     LCM Challenge Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  5. acdream.Triangles(数学推导)

    Triangles Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Stat ...

  6. acdream.A Very Easy Triangle Counting Game(数学推导)

    A - A Very Easy Triangle Counting Game Time Limit:1000MS     Memory Limit:64000KB     64bit IO Forma ...

  7. acdream.Bet(数学推导)

    Bet Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit Status Pra ...

  8. acdream.郭式树(数学推导)

    郭式树 Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  9. ACdream 1188 Read Phone Number (字符串大模拟)

    Read Phone Number Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Sub ...

随机推荐

  1. POJ2559 Largest Rectangle in a Histogram 单调栈

    题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...

  2. bzoj4737: 组合数问题

    终于过了肝了一天啊,怎么我最近都在做细节码农题啊 (这种水平NOIP凉凉??) luacs大家都可以想到用吧,一开始我的思路是把所有在p以内的%p==0的组合数预处理出来,那C(n/p,m/p)任取, ...

  3. [POJ 1041] John's Trip

    [题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...

  4. yii 面包屑

    Yii的Breadcrumbs 是Yii的路径插件,使用方法: <?php $this->widget('zii.widgets.CBreadcrumbs', array('links'= ...

  5. 洛谷P2916 [USACO08NOV]为母牛欢呼(最小生成树)

    P2916 [USACO08NOV]为母牛欢呼Cheering up the C… 题目描述 Farmer John has grown so lazy that he no longer wants ...

  6. Cracking the Coding Interview 6.5

    There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. If it ...

  7. chapter6 数据结构基础之习题 Parentheses Balance

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  8. C - Between the Offices

    Problem description As you may know, MemSQL has American offices in both San Francisco and Seattle. ...

  9. 一种压缩图片的方法---Machine learning 之 K-Means

    背景描述: RGB编码:对于一个直接用24bit表示每一个而像素的图像来说,每一个pixel使用8-bit无符号整数(0-255)来表示红or绿or蓝. 压缩目的: 将128x128大小的图片由原来的 ...

  10. sql 添加列并设置默认值

    ALTER TABLE tablsename ADD fieldname BIT NULL DEFAULT