先上题目:

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. java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决

    (1)我用的是fragment,在onStop但是没有onDestroy的情况下切换(replace)fragment时报 java.lang.IllegalStateException: Can n ...

  2. 【POJ 3076】 Sudoku

    [题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...

  3. Rails5 关联表格搜索

    创建: 2017/08/13   other_type_car = Car.joins(:car_type).active.find_by(car_type: car_type)   @recomme ...

  4. 浅谈自学Python之路(day3)

    今天的主要内容是: 撒 文件操作 对文件操作的流程: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: tonghuazhen 听说白雪公主在逃跑 小红帽在担心 ...

  5. Github标星4W+,热榜第一,如何用Python实现所有算法

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 来源 | 大数据文摘(BigDataDigest) 编译 | 周素云.蒋宝尚 学会了 Python 基 ...

  6. rabbitmq镜像模式初体验

    rabbitmq-01: yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm wget ...

  7. sql--Truncate Table

    Truncate Table(截断表) 有时候需要清除一个表中的所有资料.要达到者个目的,一种方式是DROP TABLE 指令.不过这样整个表格就消失,而无法再被用了. 另一种方式是Delete不带w ...

  8. poj3669 广搜

    //好久没刷题了,生疏了. 题意分析: 题意理解为在一个二维的正向坐标轴上,一个点(流星)连同它的上下左右的四个点会在某一个时刻被破坏.一个人在原点,问她到达安全区的最小时间是多少. 代码思路: 从原 ...

  9. Android高亮TextView

    HighlightTextView Android文本高亮控件,基于View实现. 特点 文本高亮 单词自动换行 高亮词组保持在同一行显示 截图 Demo Java: public class Mai ...

  10. C# 不卡屏延时方法,延迟系统时间,但系统又能同时能执行其它任务

    //延迟系统时间,但系统又能同时能执行其它任务,不卡屏延时方法 public static void Delay(int milliSecond) { int start = Environment. ...