zoukankan      html  css  js  c++  java
  • HDU——T The King’s Problem

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

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3254    Accepted Submission(s): 1151


    Problem Description
    In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state. What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state. And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.
      Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
     
    Input
    The first line contains a single integer T, the number of test cases. And then followed T cases. 

    The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.
     
    Output
    The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.
     
    Sample Input
    1 3 2 1 2 1 3
     
    Sample Output
    2
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3863 3859 3868 3865 3862 
     
     
    Tarjan缩点+最大独立集(强连通个数-最大匹配数)
      1 #include <cstring>
      2 #include <cstdio>
      3 
      4 #define min(a,b) (a<b?a:b)
      5 #define max(a,b) (a>b?a:b)
      6 const int N(5000+115);
      7 const int M(100000+5);
      8 int hed[N],sumedge,had[N];
      9 struct Edge
     10 {
     11     int v,next;
     12     Edge(int v=0,int next=0):v(v),next(next){}
     13 }edge[M],e[M];
     14 inline void ins(int u,int v,int *head,Edge *edge)
     15 {
     16     edge[++sumedge]=Edge(v,head[u]);
     17     head[u]=sumedge;
     18 }
     19 
     20 int tim,dfn[N],low[N];
     21 int top,instack[N],Stack[N];
     22 int sumcol,col[N],rd[N],cd[N];
     23 void DFS(int u)
     24 {
     25     low[u]=dfn[u]=++tim;
     26     Stack[++top]=u; instack[u]=1;
     27     for(int v,i=hed[u];i;i=edge[i].next)
     28     {
     29         v=edge[i].v;
     30         if(!dfn[v]) DFS(v), low[u]=min(low[u],low[v]);
     31         else if(instack[v]) low[u]=min(low[u],dfn[v]);
     32     }
     33     if(low[u]==dfn[u])
     34     {
     35         col[u]=++sumcol;
     36         for(;u!=Stack[top];top--)
     37         {
     38             col[Stack[top]]=sumcol;
     39             instack[Stack[top]]=0;
     40         }
     41         instack[u]=0; top--;
     42     }
     43 }
     44 
     45 int sumvis,vis[N],match[N];
     46 bool find(int u)
     47 {
     48     for(int v,i=had[u];i;i=e[i].next)
     49     {
     50         v=e[i].v;
     51         if(vis[v]==sumvis) continue;
     52         vis[v]=sumvis;
     53         if(!match[v]||find(match[v]))
     54         {
     55             match[v]=u;
     56             return true;
     57         }
     58     }
     59     return false;
     60 }
     61 
     62 inline void init()
     63 {
     64     tim=top=sumedge=sumcol=sumvis=0;
     65     memset(e,0,sizeof(e));
     66     memset(vis,0,sizeof(vis));
     67     memset(col,0,sizeof(col));
     68     memset(dfn,0,sizeof(dfn));
     69     memset(low,0,sizeof(low));
     70     memset(hed,0,sizeof(hed));
     71     memset(had,0,sizeof(had));
     72     memset(edge,0,sizeof(edge));
     73     memset(Stack,0,sizeof(Stack));
     74     memset(match,0,sizeof(match));
     75     memset(instack,0,sizeof(instack));
     76 }
     77 inline void read(int &x)
     78 {
     79     x=0; register char ch=getchar();
     80     for(;ch>'9'||ch<'0';) ch=getchar();
     81     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
     82 }
     83 
     84 int main()
     85 {
     86     int t; read(t);
     87     for(int n,m;t--;init())
     88     {
     89         read(n),read(m);
     90         for(int u,v;m--;)
     91             read(u),read(v),ins(u,v,hed,edge);
     92         for(int i=1;i<=n;i++)
     93             if(!dfn[i]) DFS(i);
     94         for(int u=1;u<=n;u++)
     95             for(int v,i=hed[u];i;i=edge[i].next)
     96             {
     97                 v=edge[i].v;
     98                 if(col[u]!=col[v]) ins(col[u],col[v],had,e);
     99             }
    100         int ans=0;
    101         for(int i=1;i<=sumcol;i++)
    102         {
    103             sumvis++;
    104             if(find(i)) ans++;
    105         }
    106         printf("%d
    ",sumcol-ans);
    107     }
    108     return 0;
    109 }
     
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    自定义UILabel的对齐方式
    获取iOS系统版本 --- UIDevice的使用
    iOS 照片多选
    iOS 手势+触摸事件
    IOS消息推送
    设置tableViewCell的背景颜色
    IOS_修改TableView的删除按钮的文本
    UIScrollView控件实现轮播图
    判断设备是不是第一次进入应用
    英语口语
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7470007.html
Copyright © 2011-2022 走看看