NC51317. Air Raid
描述
输入描述
Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:
no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street consists of two positive integers, separated by one blank: - the number of the intersection that is the start of the street, and - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.
输出描述
The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town.
示例1
输入:
2 4 3 3 4 1 3 2 3 3 3 1 3 1 2 2 3
输出:
2 1
C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 476K, 提交时间: 2022-08-31 22:13:20
#include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f #define mod 10000007 #define debug() puts("what the fuck!!!") #define ll long long using namespace std; const int N=1600+20; int vis[N],match[N],n,m; vector<int> G[N]; int dfs(int u) { for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(!vis[v]) { vis[v]=1; if(!match[v]||dfs(match[v])) { match[v]=u; return 1; } } } return 0; } int query() { mem(match,0); int sum=0; for(int i=1;i<=n;i++) { mem(vis,0); if(dfs(i)) sum++; } return sum; } int main() { int t,a,b; scanf("%d",&t); while(t--) { mem(G,0); scanf("%d%d",&n,&m); while(m--) { scanf("%d%d",&a,&b); G[a].push_back(b); } printf("%d\n",n-query()); } return 0; }
C++ 解法, 执行用时: 3ms, 内存消耗: 320K, 提交时间: 2021-09-09 19:33:15
#include <bits/stdc++.h> using namespace std; #define maxn 150 int uN, vN; bool g[maxn][maxn]; int xM[maxn], yM[maxn]; bool chk[maxn]; int n, m; bool SearchPath(int u) { int v; for (v =0; v < vN; v++) if (g[u][v] &&!chk[v]) { chk[v] =true; if (yM[v] ==-1|| SearchPath(yM[v])) { yM[v] = u; xM[u] = v; return true; } } return false; } int MaxMatch() { int u, ret =0; memset(xM, -1, sizeof(xM)); memset(yM, -1, sizeof(yM)); for (u =0; u < uN; u++) if (xM[u] ==-1) { memset(chk, false, sizeof(chk)); if (SearchPath(u)) ret++; } return ret; } void input() { memset(g, 0, sizeof(g)); scanf("%d%d", &n, &m); uN = vN = n; for (int i =0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; g[a][b] =true; } } int main() { // freopen("t.txt", "r", stdin); int t; scanf("%d", &t); while (t--) { input(); int ans = n - MaxMatch(); printf("%d\n", ans); } return 0; }