Hdoj Topic 3861 The King's problem (strong connectivity, minimum point coverage)

Source: Internet
Author: User

The King ' s problemTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1830 Accepted Submission (s): 666


Problem DescriptionIn The Kingdom of Silence, the king has a new problem. There is N cities in the kingdom and there is M directional roads between the cities. That means that if there are a road from U to V, and you can only go from the city u to City v, and can ' t go from the City v to the city U. In order to rule he kingdom more effectively, the king want to divide he kingdom into several states, and all city mus T belong to exactly one state.What's more , for each pair of the city (U, v), if there is one-to-go from U-V and go from V to U, (U, v) has to be Long 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-your help, he wants to know the least number of states he has to divide the kingdom into.
Inputthe first line contains a single integer T, the number of test cases. And then followed T cases.

The first line for each case contains integers n, m (0 < n <= 5000,0 <= m <= 100000), the number of cities And roads in the kingdom. The next m lines each contains the integers u and V (1 <= u, v <= N), indicating that there was a road going from CIT Y u to City v.
Outputthe output should contain T lines. For each test case, you should just output a integer which is the least number of states the king has to divide into.
Sample Input
13 21 21 3

Sample Output
2

Source2011 multi-university Training Contest 3-host by BIT
Recommendlcy | We have carefully selected several similar problems for you:3859 3868 3865 3862 3866 AC Code
#include <stdio.h> #include <string.h> #include <vector> #include <string> #include < Iostream> #define MAX (A, B) (a>b?a:b) #define MIN (A, b) (a>b?b:a) using namespace Std;int dfn[5050],low[5050],ins [5050],head[5050],belong[5050],stack[5050],link[5050];int cnt,top,taj,time,n,m;vector<int>vt[5050];struct S{int U,v,next;} Edge[100010*2];void init () {Cnt=top=taj=time=0;memset (belong,-1,sizeof (belong)); memset (ins,0,sizeof (INS)); memset (Low,-1,sizeof (Low)); Memset (Dfn,-1,sizeof (DFN)); Memset (Head,-1,sizeof (head)); memset (stack,0,sizeof (stack));} void Add (int u,int v) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt++;} void Tarjin (int u) {dfn[u]=low[u]=time++;stack[top++]=u;ins[u]=1;for (int i=head[u];i!=-1;i=edge[i].next) {int V=edge [I].v;if (Dfn[v]==-1) {Tarjin (v); Low[u]=min (Low[u],low[v]);} ElseIf (Ins[v]) low[u]=min (Dfn[v],low[u]);} if (Dfn[u]==low[u]) {Taj++;int now;do{now=stack[--top];belong[now]=taj;ins[now]=0;} while (U!=now);}} int dfs (int u) {for (int i=0;i<vt[U].size (); i++) {int v=vt[u][i];if (!ins[v]) {ins[v]=1;if (link[v]==-1| | DFS (Link[v])) {Link[v]=u;return 1;}}} return 0;} int main () {int t;scanf ("%d", &t), while (t--) {int. i;init (); scanf ("%d%d", &n,&m); for (i=1;i<=n;i++) Vt[i] . Clear (); while (m--) {int u,v;scanf ("%d%d", &u,&v); add (u,v);} for (i=1;i<=n;i++) {if (dfn[i]==-1) {Tarjin (i);}} for (i=0;i<cnt;i++) {int u=edge[i].u;int v=edge[i].v;if (Belong[u]!=belong[v]) {vt[belong[u]].push_back (Belong[v] );//printf ("%d%d%d%d\n", U,v,belong[u],belong[v]);}} memset (link), int ans=0;for (i=1;i<=taj;i++) {memset (ins,0,sizeof (INS)), if (Dfs (i)) ans++;} printf ("%d\n", Taj-ans);}}


Hdoj Topic 3861 The King's problem (strong connectivity, minimum point coverage)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.