HDU 1845 Jimmy’s Assignment (求最大匹配数)

Jimmy’s Assignment

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2405    Accepted Submission(s): 968


Problem Description
Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore, the graph is 2-edge-connected (that is, at least 2 edges need to be removed in order to make the graph disconnected). A matching is a subset of the graph’s edges, such that no two edges in the subset have a common vertex. A maximum matching is a matching having the maximum cardinality.
  Given a series of instances of the special graph mentioned above, find the cardinality of a maximum matching for each instance.
 

 

Input
The first line of input contains an integer number T, representing the number of graph descriptions to follow. Each description contains on the first line an even integer number N (4<=N<=5000), representing the number of vertices. Each of the next 3*N/2 lines contains two integers A and B, separated by one blank, denoting that there is an edge between vertex A and vertex B. The vertices are numbered from 1 to N. No edge may appear twice in the input.
 

 

Output
For each of the T graphs, in the order given in the input, print one line containing the cardinality of a maximum matching.
 

 

Sample Input
2 4 1 2 1 3 1 4 2 3 2 4 3 4 4 1 2 1 3 1 4 2 3 2 4 3 4
 

 

Sample Output
2 2
 

 

Author
Mugurel Ionut Andreica
 

 

Source
 
其实这题可以取巧
边的数量为顶点n*3/2
如果把顶点平均分成两部分,让左边的每一个点都连接右边的点,边的数量正好是n*3/2
所以,最大匹配数就是n/2
以下代码
#include<iostream>
using namespace std;int main(){int t,n,a,b,i;cin>>t;while(t--){cin>>n;for(i=0;i<n*3/2;i++)cin>>a>>b;cout<<n/2<<endl;}return 0;}

咳咳,我写成这样主要是为了代码长度那显示的短,只有146B……

以下为整理了格式的

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     int t, n, a, b, i;
 7     cin >> t;
 8     while (t--) {
 9         cin >> n;
10         for (i = 0; i < n * 3 / 2; i++)cin >> a >> b;
11         cout << n / 2 << endl;
12     }
13     return 0;
14 }

这题可以用匈牙利算法做

 

posted @ 2019-07-19 10:13  颤抖的方便面  阅读(142)  评论(0编辑  收藏  举报