poj 3713 Transferring Sylla 判断无向图是否每两个点之间都能至少有3点不同的路径可达 是否3-连通

After recapturing Sylla, the Company plans to establish a new secure system, a transferring net! The new system is designed as follows:

The Company staff choose N cities around the nation which are connected by "security tunnels" directly or indirectly. Once a week, Sylla is to be transferred to another city through the tunnels. As General ordered, the transferring net must reach a certain security level that there are at least 3 independent paths between any pair of cities a, b. When General says the paths are independent, he means that the paths share only a and b in common.

Given a design of a transferring net, your work is to inspect whether it reaches such security level.

Input

The input consists of several test cases.
For each test case, the first line contains two integers, N ≤ 500 and M ≤ 20000. indicating the number of cities and tunnels.
The following M lines each contains two integers a and b (0 ≤ a, b < N), indicating the city a and city b are connected directly by a tunnel.

The input ends by two zeroes.

Output

For each test case output "YES" if it reaches such security level, "NO" otherwise.

Sample Input

4 6
0 1
0 2
0 3
1 2
1 3
2 3

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

7 6
0 1
0 2
0 3
1 2
1 3
2 3

0 0
Sample Output

YES
NO
NO

 

 

 

 

 

 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=550;
//给出一个无向图(可能不联通),判断是否每两个点之间都能至少有3点不同的路径可达,
//即该图是否至少是3-连通图,即至少去掉3个点图才不联通
//枚举删去一个点,看是否还存在割点,如果存在割点,则不是3连通图
//tarjan算法求无向连通图的割点个数
struct edge
{
 int t,w;
 int next;
};
int V,E;//顶点个数 边数
int p[maxn];
edge G[42000];
int l;
void init()
{
 memset(p,-1,sizeof(p));
 l=0;
}
void addedge(int u,int t,int w,int l)
{
 G[l].w=w;
 G[l].t=t;
 G[l].next=p[u];
 p[u]=l;
}
int del;
//tarjan 求割点 割边
int cut[maxn];//cut[i]非0表示i是割点
int color[maxn];//颜色:0表示没有访问,1表示正在访问,2表示访问结束
int lowc[maxn];//表示i及i的子孙相连的辈分最高的祖先节点所在的深度
int d[maxn];//表示i节点在树中的深度
int root;//根节点
int fath;//父节点
int pcnt;//割点个数
int egcnt;//割边个数
int flag;//是否存在割点 1:有  0:没有
void dfs(int u,int fath,int deep)
{
    if(flag) return ;
    color[u]=1;//正在访问
    lowc[u]=d[u]=deep;//深度
    int tot=0;//子树个数
    for(int i=p[u];i!=-1;i=G[i].next)
    {
        int t=G[i].t;
        if(t!=fath&&color[t]==1)
        {
            lowc[u]=min(lowc[u],d[t]);
        }
        if(color[t]==0)
        {
            dfs(t,u,deep+1);
            tot++;//子树加1
            lowc[u]=min(lowc[u],lowc[t]);
            //求割点
            if((u==root&&tot>1)||(u!=root&&lowc[t]>=d[u])) cut[u]=1,flag=1;//不能将pscnt++写到这里
            //求割边
            //if(lowc[t]>d[u]) edge[u][t]=true;  u->t是割边
        }
    }
    color[u]=2;
}
void calc()
{
    pcnt=egcnt=0;
    memset(cut,0,sizeof(cut));
    memset(color,0,sizeof(color));color[del]=2;
    memset(lowc,0,sizeof(lowc));
    memset(d,0,sizeof(d));
    root=1;
    if(del==1) root=2;
    dfs(root,-1,1);
    //for(int i=1;i<=V;i++) if(cut[i]) pcnt++;
}
int main()
{
 while(scanf("%d%d",&V,&E)==2)
    {
        if(V==0&&E==0) break;
     init();//初始化
        for(int i=0;i<E;i++)
        {
            int u,t,w=1;
            scanf("%d%d",&u,&t);u++,t++;
            addedge(u,t,w,l++);
            addedge(t,u,w,l++);
        }
        flag=0;
        for(int i=1;i<=V;i++)
        {
            del=i;
            calc();//求割点
            for(int j=1;j<=V;j++)
            {
                if(color[j]==0)
                {
                    flag=1;
                    break;
                }
            }
            if(flag) break;
        }
        if(flag) printf("NO\n");
        else printf("YES\n");
 }
 return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值