Bzoj1143: [ctsc2008] Sacrifice to River

Source: Internet
Author: User
1143: [ctsc2008] Sacrifice rivertime limit: 10 sec memory limit: 162 MB
Submit: 984 solved: 490
[Submit] [Status] Description

In the Far East, there is a mysterious nation, claiming to be the Y family. They lived on the water for generations, and the Dragon King was God. During Major celebrations, the yth family will hold a grand sacrificial activity on the water. We can regard the water system of the Y-family residence as a network composed of a fork and a river. Each river is connected with two forks, and the water flows in a fixed direction in the river. Apparently, there is no circulation in the water system (an example of a circulation ). Due to the large number of people, the sacrifices of the yth family will be held at the same time on Multiple forks. Out of respect for the Dragon King, these sacrifices must be carefully selected. Accurately speaking, the yth people believe that if water flow can flow from one sacrifice point to another, the sacrifice will lose its sacred meaning. The Patriarch hopes to select as many places as possible based on the sacredness of sacrifices.

Input

The first line contains two integers n and m separated by spaces, indicating the number of forks and rivers respectively. The number of forks ranges from 1 to n. Next, in the M line, each line contains two integers u and v separated by spaces. This describes a river that connects the U and V of the fork, and the flow direction is from u to v.

Output

The first line contains an integer k, indicating the maximum number of sacrificial points that can be selected.

Sample input4 4
1 2
3 4
3 2
4 2
Sample output2

[Example]
In the water system given in the example, there is no way to select three or more sacrifice points. There are two kinds of test points that contain two sacrifice points:
Select fork 1 and fork 3 (for example, the second line is output), and select fork 1 and fork 4.
Water Flow can flow from any fork to fork 2. If a sacrifice point is set up at fork 2, no other sacrifices can be set up.
However, we can establish two sacrifice points in the optimal selection scheme of a sacrifice point, so the sacrifice point cannot be set up at the entrance 2. For other forks
There must be at least one optimal solution, so this fork is a sacrifice point, so the output is 1011.
Hint

 

For each test point: if you only output the correct number of selected sacrifice points, you will get a 30% score for the test point; if you only output the correct number of selected sacrifice points and a feasible solution, you will get a 60% score for the test point. If your output is completely correct, then you will get a score of 100% for this test point. [data scale] n ≤ 100 m ≤ 1 000

Question: after seeing the question, I thought, isn't it the largest independent set after floyed? Alas? No. Is this a bipartite graph? I don't feel like I have read the question: Split the point into a bipartite graph... A mouthful of blood sprayed out... After the split, is it a bipartite graph? Haha... Do not understand the code:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #define inf 100000000012 #define maxn 11013 #define maxm 500+10014 #define eps 1e-1015 #define ll long long16 #define pa pair<int,int>17 using namespace std;18 inline int read()19 {20     int x=0,f=1;char ch=getchar();21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}22     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}23     return x*f;24 }25 int p[maxn],n,m,f[maxn][maxn];26 bool v[maxn];27 bool find(int x)28 {29     for(int i=1;i<=n;i++)30     if (f[x][i])31      if(!v[i])32      {33          v[i]=1;34          if(!p[i]||find(p[i]))35          {36              p[i]=x;37              return 1;38          }39      }40     return 0; 41 }42 int main()43 {44     freopen("input.txt","r",stdin);45     freopen("output.txt","w",stdout);46     n=read();m=read();47     int x,y;48     while(m--)x=read(),y=read(),f[x][y]=1;49     for(int k=1;k<=n;k++)50      for(int i=1;i<=n;i++)51       for(int j=1;j<=n;j++)52        f[i][j]=f[i][j]||(f[i][k]&&f[k][j]);53     int ans=0;  54     for(int i=1;i<=n;i++)55      {56          memset(v,0,sizeof(v));57          if(find(i))ans++;58      }   59     printf("%d\n",n-ans);60     return 0; 61 }
View code

 

Bzoj1143: [ctsc2008] Sacrifice to River

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.