hdu - 1704 Rank(简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1704

遇到标记过的就dfs,把隐含的标记,最后计数需要注意。

 1 #include <cstdio>
 2 #include <cstring>
 3 int n;
 4 int vis[501][501];
 5 void dfs(int x,int y)
 6 {
 7     for(int i=1;i<=n;i++)
 8         if(vis[y][i])
 9         {
10             vis[x][i]=1;
11             dfs(x,i);
12         }
13 }
14 int main()
15 {
16    // freopen("a.txt","r",stdin);
17    int t,m,a,b;
18    scanf("%d",&t);
19    while(t--)
20    {
21        memset(vis,0,sizeof(vis));
22        scanf("%d%d",&n,&m);
23        for(int i=0;i<m;i++)
24        {
25            scanf("%d%d",&a,&b);
26            vis[a][b]=1;
27        }
28        for(int i=1;i<=n;i++)
29             for(int j=1;j<=n;j++)
30             if(vis[i][j])
31             {
32                 dfs(i,j);
33             }
34         int ans=0;
35         for(int i=1;i<=n;i++)
36             for(int j=i+1;j<=n;j++)
37             if(!vis[i][j]&&!vis[j][i]) ans++;
38         printf("%d\n",ans);
39    }
40    return 0;
41 }

也可以用有向图传递闭包的方法。实质是一样的。

 1 #include <cstdio>
 2 #include <cstring>
 3 int vis[501][501];
 4
 5 int main()
 6 {
 7    // freopen("a.txt","r",stdin);
 8    int t,n,m,a,b;
 9    scanf("%d",&t);
10    while(t--)
11    {
12        memset(vis,0,sizeof(vis));
13        scanf("%d%d",&n,&m);
14        for(int i=0;i<m;i++)
15        {
16            scanf("%d%d",&a,&b);
17            vis[a][b]=1;
18        }
19        for(int k=1;k<=n;k++)
20        for(int i=1;i<=n;i++)
21             if(vis[i][k])
22             {
23                 for(int j=1;j<=n;j++)
24                 vis[i][j]=vis[i][j]||vis[k][j];
25             }
26         int ans=0;
27         for(int i=1;i<=n;i++)
28             for(int j=i+1;j<=n;j++)
29             if(!vis[i][j]&&!vis[j][i]) ans++;
30         printf("%d\n",ans);
31    }
32    return 0;
33 }
时间: 2024-11-17 01:15:18

hdu - 1704 Rank(简单dfs)的相关文章

hdu 1704 Rank(floyd传递闭包)

题目链接:hdu 1704 Rank 题意: 有n个人,m场比赛,a与b打,每场都是awin,问不能确定其中两个人的win情况数. 题解: floyd传递闭包,这里我用bitset优化了一下. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 bitset<501>a[501]; 5 6 int n,m,t; 7 8 int main(){ 9

HDU 1704 Rank

Rank Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 170464-bit integer IO format: %I64d      Java class name: Main there are N ACMers in HDU team.ZJPCPC Sunny Cup 2007 is coming, and lcy want to select some

HDU 1704 Rank【传递闭包】

解题思路:给出n个选手,m场比赛,问不能判断胜负的询问最多有多少种 用传递闭包即可 但是如果直接用3重循环会超时 在判断d[i][j]=d[i][k]||d[k][j]是否连通的时候 可以加一个if语句判断一下d[i][k]是否为1,为1再进行第三重循环,不为1则不进行第三次循环 反思:例如询问 3和1,1和3是相同的情况,所以最后判断的时候,只需要判断上三角矩阵即可. Rank Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

hdu 4739Zhuge Liang&#39;s Mines(简单dfs,需要注意重点)

Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1166    Accepted Submission(s): 505 Problem Description In the ancient three kingdom period, Zhuge Liang was the most famous

hdu 1520Anniversary party(简单树形dp)

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4310    Accepted Submission(s): 1976 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

Counting Cliques HDU - 5952 单向边dfs

题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月5号的计蒜客重现赛只给了1000ms确实有点过分吧,好久没有做这种简单dfs做到自闭了,,,题目并不难,注意剪枝就好了,建图时建标号小的点指向标号大的点的单向边,这样按标号从小到大搜一遍就好了,完全图的任意两个点都要有边,按点的标号搜到第n-s+1个点,因为后面所有的点加起来都组不成点数为s的完全子

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的