hdu6029 Graph Theory (2017女生赛)

Graph Theory

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 167    Accepted Submission(s): 84


Problem Description
Little Q loves playing with different kinds of graphs very much. One day he thought about an interesting category of graphs called ``Cool Graph'', which are generated in the following way:
Let the set of vertices be {1, 2, 3, ..., n }. You have to consider every vertice from left to right (i.e. from vertice 2 to n ). At vertice i , you must make one of the following two decisions:
(1) Add edges between this vertex and all the previous vertices (i.e. from vertex 1 to i1 ).
(2) Not add any edge between this vertex and any of the previous vertices.
In the mathematical discipline of graph theory, a matching in a graph is a set of edges without common vertices. A perfect matching is a matching that each vertice is covered by an edge in the set.
Now Little Q is interested in checking whether a ''Cool Graph'' has perfect matching. Please write a program to help him.

Input
The first line of the input contains an integer T(1T50) , denoting the number of test cases.
In each test case, there is an integer n(2n100000) in the first line, denoting the number of vertices of the graph.
The following line contains n1 integers a2,a3,...,an(1ai2) , denoting the decision on each vertice.

Output
For each test case, output a string in the first line. If the graph has perfect matching, output ''Yes'', otherwise output ''No''.

Sample Input
  
  
3 2 1 2 2 4 1 1 2
 
Sample Output
  
  
Yes No No
 
Source
 

再说题意之前我要先学个单词:

所以说 a set of 的意思是 一组...
只需找到的是一组边使得满足perfect matching!

题意:有n个点让你进行操作,从第二点开始,你可以开始操作,并且只能操作其中的任意一个,输入给出了点数和各点的操作,现在要求的就是是否有一组边满足perfect matching;perfect matching 的意思就是任何一个点当且仅当被一条边所覆盖;
两种操作分别是,1:当前点和所有前面的点都要连;2:当前点与前面任意点都不去相连!
思路:完美匹配就是: 。——。 。——。...... 这种方式;所以点数必须为偶数个。第一个点没有操作,可以假设它的操作为2,然后后面遇到一个1就和前面的2匹配一下,作何如果都匹配成功,就ok啦!
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
typedef long long ll;
const ll tomod=1e9+7;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,x;
        int flag2=1;
        scanf("%d",&n);

        for(int i=2;i<=n;i++)
        {
            scanf("%d",&x);
            if(x==1)
            {
                if(flag2>0)
                    flag2--;
                else
                    flag2++;
            }
            else if(x==2)
                flag2++;
        }

        if(flag2!=0||n%2!=0)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}


### 关于割点和割边的算法练习题 #### 割点与割边的概念 在图论中,割点是指如果删除某个顶点及其关联的所有边之后,原连通图被分割成两个或更多不相连的部分,则该顶点称为割点。同样地,割边(也称桥)是指当删除某条边后,原连通图也被分割成两部分或多部分[^4]。 以下是几个经典的割点和割边相关题目集合: --- #### 经典题目集合 1. **POJ 3177 Redundant Paths** - 描述:给定一个无向图,询问至少需要添加多少条边才能使整个图变成双连通图。 - 解法提示:利用 Tarjan 算法找到所有的桥,并通过计算森林中的叶子节点数量来推导所需增加的最少边数[^5]。 2. **UVA 10189 Minesweeper (改编版本)** - 描述:在一个网格地图上模拟炸弹爆炸的影响范围,其中某些位置可能成为割点或者割边的关键路径。 - 解法提示:可以扩展为动态规划加 DFS 的形式,重点在于如何快速定位哪些区域会因为特定点/边而断开连接。 3. **Codeforces Round #XYZ Problem A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z** - 多次比中都出现了基于 Tarjan 算法的应用实例,比如 CF 场景下的 “Bridge Finding” 或者类似的变种问题。 4. **LeetCode Premium Problems** - 虽然 LeetCode 上免费资源较少提及显式的“割点”概念,但在一些高级付费课程里确实存在针对此类主题设计的教学案例,尤其是涉及到复杂网络拓扑结构分析时更为明显。 5. **HDU OJ Series on Graph Theory Challenges** - HDU 平台提供了大量围绕图理论展开的实际操作型习题集锦,其中包括但不限于检测简单路径上的所有潜在瓶颈——也就是我们所说的割点和桥梁。 ```python def find_bridges(graph, n): result = [] visited = [False]*n disc = [-1]*n low = [-1]*n parent = [-1]*n time = [0] def dfs(u): children = 0 visited[u] = True disc[u] = low[u] = time[0] time[0] += 1 for v in graph[u]: if not visited[v]: parent[v] = u children += 1 dfs(v) low[u] = min(low[u], low[v]) if low[v] > disc[u]: result.append((u,v)) elif v != parent[u]: low[u] = min(low[u], disc[v]) for i in range(n): if not visited[i]: dfs(i) return result ``` 上述代码片段展示了如何运用深度优先搜索技术去发现存在于任意无向图内的全部桥梁列表。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值