HDU 6029 Graph Theory

贪心。

每次找到后面最近的一个能连边的连边。

#include <bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
int T,n,k;
int a[123456],b[123456],u[123456];

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int cnt=0;
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==1) cnt++;
        }
        k=0;
        for(int i=n;i>=2;i--)
        {
            if(a[i]==1) b[k++]=i;
        }
        int ok=1;
        memset(u,0,sizeof(u));
        k--;
        for(int i=1;i<=n;i++)
        {
           if(u[i]==1) continue;
           if(b[k]==i) {k--;}
           if(k==-1){ ok=0;break;}
            u[b[k]]=u[i]=1;
            k--;
        }
        if(ok) puts("Yes");
        else puts("No");
    }
    return 0;
}
时间: 2024-08-09 02:17:28

HDU 6029 Graph Theory的相关文章

2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 830 Problem Description Ther

HDU 4034 Graph(floyd,最短路,简单)

题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXN=110; const int INF=0x3f3f3f3f;//防止后面溢出,这个不能太大 bool vis[MAXN]; int pr

HDU 3726 Graph and Queries treap树

题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; struct Node { Node *ch[2]; int r; int v; int s; Node(int v): v(v) { ch[0] = ch[1] = NULL; r = rand(); s

HDU 4467 Graph

Graph Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2618    Accepted Submission(s): 425 Problem DescriptionP. T. Tigris is a student currently studying graph theory. One day, when he was stud

2017 UESTC Training for Graph Theory

2017 UESTC Training for Graph Theory A       思维 题意:给你一个有n个点和m条边的无向连通图,每条边都有一个权值w.我们定义,对于一条路径,它的Charm value为该路径上所有边的权值的最大值与最小值的差.询问从1到n的所有路径的Charm value的最小值. tags:有点思维定式了..一条路径里只要最大最小值,所以边可以重复走.这样我们只要把边从小到大枚举,把第 i 条边作为最小边,然后对于每个 i ,我们按顺序逐一加入比它大的边,直到点

Graph Theory

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 140    Accepted Submission(s): 74 Problem Description Little Q loves playing with different kinds of graphs very much. One day he thought about

Graph Theory Algorithms

1 /** 2 * 图论 3 **/ 4 5 /***************************** 图的抽象数据类型 ************************************/ 6 ADT Graph 7 { 8 数据: 9 Graph = (Vertex, Edge)是可以用不同方式存储的图,Vertex是顶点集, 10 Edge = {<vertex_1, vertex_2> | vertex_1, vertex_2属于Vertex,vertex_1不等于verte

hdu 3726 Graph and Queries(splay查询第k大,启发式合并,删除操作)

题目链接:hdu 3726 Graph and Queries 题意: 最开始给你n个点,每个点最开始有一个权值,并且都是独立的,现在给你m条边,表示对应的两个点是连接的. 现在有三种操作: Q x k,表示询问与x这个点联通的所有点中第k大的权值. D x,表示删除第x条边. C x y,表示改变x点的权值为y. 题解: 如果正着来做肯定比较麻烦,不好维护. 一看输出,可以离线处理,那么我们就倒着搞. 把所有的询问和边,点的变化全部存起来,然后用splay来维护. 每次删除就是倒着的合并,合并

hdu 4034 Graph

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1650    Accepted Submission(s): 826 Problem Des