先上题目:

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

2 1
1 2
5 4
1 2
1 3
1 4
1 5

Sample Output

NO
YES   根据题意,我们可以将点分成3种:①度小于3的点,②度等于3的点,③度大于等于4的点。
  对于①,我们可以直接跳过,因为这种点无论是单个还是组合都无法产生符合要求的子图。对于②,如果有两个度为三的点连载一起并且重合的点小于等于1个的话就有可能产生符合要求的子图。对于③,一个点就可以引出符合要求的子图。
  所以我们可以先判断是否有③的点,如果有就直接输出YES,否则判断所有度为③的点是否有符合要求的,如果有就直接输出YES,否则就不存在题目要求的子图。 上代码:
 #include <cstdio>
#include <cstring>
#define MAX 100002
using namespace std; int c[MAX][],p[MAX],d[MAX],a[MAX],co,n,m; int findset(int u){
return u==p[u] ? u : p[u]=findset(p[u]);
} bool check_(int x,int y){
int ans=;
for(int i=;i<;i++){
if(c[x][i]==y) ans++;
else{
for(int j=;j<;j++){
if(c[x][i]==c[y][j]) ans++;
}
}
}
return ans<=;
} bool check(){
co=;
for(int i=;i<n;i++){
if(d[i]>=) return ;
else if(d[i]==) a[co++]=i;
}
for(int i=;i<co;i++){
for(int j=i+;j<co;j++){
if(findset(a[i])==findset(a[j]) && check_(a[i],a[j])) return ;
}
}
return ;
} int main()
{
int u,v;
//freopen("data.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF){
for(int i=;i<=n;i++) p[i]=i;
memset(d,,sizeof(d));
for(int i=;i<m;i++){
scanf("%d %d",&u,&v);
if(d[u]<) c[u][d[u]]=v;
if(d[v]<) c[v][d[v]]=u;
d[u]++; d[v]++;
u = findset(u);
v = findset(v);
if(u!=v) p[v]=p[u];
}
if(check()) printf("YES\n");
else printf("NO\n");
}
return ;
}

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. oc31--new实现

    // // main.m // new方法实现原理 #import <Foundation/Foundation.h> #import "Person.h" int m ...

  2. 海思3518e mpp2/sample/venc makefile简析

    http://blog.csdn.net/u011003120/article/details/51324567

  3. C#操作INI文件(明天陪你看海)

    C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...

  4. B1257 [CQOI2007]余数之和 数学,分块

    这个题想明白之后很好做,但是不好想.我根本没想出来,上网看了一下才知道怎么做... 这个题其实得数是一个等差数列,然后一点点求和就行了. 上次NOIP就是没看出来规律,这次又是,下次先打表找规律!!! ...

  5. Flink之DataStreamAPI入门

    目录 Types Transformations Defining UDFs 本文API基于Flink 1.4 def main(args: Array[String]) { // 第一种会自动判断用 ...

  6. java中强制类型转换时,高位数截取成低位数的方法

    /** * 强制类型转换中的补码.反码.原码一搞清楚 */ int b=233;//正整数强转 System.out.println((byte)b); //负数:原码的绝对值取反再加一 符号为不变 ...

  7. C#之仿魔兽登录

    不多废话,直接上效果图: 1录窗体 对应的代码: using System; using System.Collections.Generic; using System.ComponentModel ...

  8. 文字水平居中和垂直居中的CSS

    首先选择一个需要显示文字的选择器,我这里选择的是微信小程序里面的<view>选择器,在其他语言(如html)的选择器里是一样的做法: <view class="btn-it ...

  9. javascript基础(完整)

    一.什么是javascript? 是一种基于对象和事件驱动(以事件驱动的方式直接对客户端的输入做出响应,无需经过服务器端)并具有安全性能的解释型脚本语言,在web应用中得到非常广泛地应用.它不需要编译 ...

  10. response.getWriter().write()乱码问题

    前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...