hdu1704 transitive closure

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

Transitive closure on the relationship set R defines, if both the tuple (x, y) there tuple (y, z) is a tuple (x, z) in relation R, thus having a minimum set of relationships is transitive closure. There are explained: transitive closure, i.e., in mathematics, transmitting binary relation R in the set X closure smallest transitive relation R on X comprises a. For example, if X is (dead or alive) and R is the set of human relations "parent-child", the R transitive closure of the relationship "x y is the ancestor." As another example, if X is set to xRy airports and the relationship "x y from airport to airport with direct flights", the transitive closure of R is "probably by one or more navigation fly y from x."

This question we can define the relationship in X set (a set of numbers) "win", then the relationship is transitive nature. Can floyd solved transitive closure, transitive closure of the transfer formula is: dis [i] [j] = dis [i] [j] | (dis [i] [k] & dis [k] [j]), which questions only pay attention to a small point is optimized triple loop.

code show as below:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define mp(a,b) make_pair((a),(b))
17 #define P pair<int,int>
18 #define dbg(args) cout<<#args<<":"<<args<<endl;
19 #define inf 0x7ffffff
20 inline int read(){
21     int ans=0,w=1;
22     char ch=getchar();
23     while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
24     while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
25     return ans*w;
26 }
27 const int maxn=1000;
28 int n,m,t;
29 int dis[maxn][maxn];
30 void floyd()
31 {
32     f(k,1,n)
33         f(i,1,n)
34         {
35             if(dis[i][k])
36             f(j,1,n)
37             {
38                 dis[i][j]=dis[i][j]|(dis[i][k]&dis[k][j]);//传递闭包 
39              } 
40         }    
41             
42  } 
43  int ans=0;
44 int main()
45 {
46     //freopen("input.txt","r",stdin);
47     //freopen("output.txt","w",stdout);
48     std::ios::sync_with_stdio(false);
49     t=read();
50     while(t--)
51     {
52         mem(dis,0);
53         ans=0;
54         n=read(),m=read();
55         int x,y;
56         while(m--)
57         {
58             x=read(),y=read();
59             dis[x][y]=1;
60         }
61         floyd();
62         F (I, . 1 , n-)
 63 is              F (J, I + . 1 , n-)
 64              {
 65                  IF ANS ++ (DIS [I] [J] && DIS [J] [I]!!); // (I, J) no association between, that is not winning or losing inferred relationship 
66              }
 67              PF ( " % D \ n- " , ANS);
 68       } 
 69 }

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12590356.html