标签:memset arch format can int class space continue script
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3800 Accepted Submission(s): 1401
Special Judge
1 //2017-08-26 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 100000; 10 const int M = 5000000; 11 int head[N], tot; 12 struct Edge{ 13 int to, next; 14 }edge[M]; 15 16 void init(){ 17 tot = 0; 18 memset(head, -1, sizeof(head)); 19 } 20 21 void add_edge(int u, int v){ 22 edge[tot].to = v; 23 edge[tot].next = head[u]; 24 head[u] = tot++; 25 26 edge[tot].to = u; 27 edge[tot].next = head[v]; 28 head[v] = tot++; 29 } 30 31 int n; 32 int matching[N]; 33 int check[N]; 34 bool dfs(int u){ 35 for(int i = head[u]; i != -1; i = edge[i].next){ 36 int v = edge[i].to; 37 if(!check[v]){//要求不在交替路 38 check[v] = 1;//放入交替路 39 if(matching[v] == -1 || dfs(matching[v])){ 40 //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功 41 matching[u] = v; 42 matching[v] = u; 43 return true; 44 } 45 } 46 } 47 return false;//不存在增广路 48 } 49 50 //hungarian: 二分图最大匹配匈牙利算法 51 //input: null 52 //output: ans 最大匹配数 53 int hungarian(){ 54 int ans = 0; 55 memset(matching, -1, sizeof(matching)); 56 for(int u = 1; u <= n; u++){ 57 if(matching[u] == -1){ 58 memset(check, 0, sizeof(check)); 59 if(dfs(u)) 60 ans++; 61 } 62 } 63 return ans; 64 } 65 66 const int MAXID = 100; 67 int a[N], b[N], cnt; 68 69 int main() 70 { 71 std::ios::sync_with_stdio(false); 72 //freopen("inputE.txt", "r", stdin); 73 while(cin>>n){ 74 init(); 75 int v; 76 for(int i = 1; i <= n; i++){ 77 for(int j = 1; j <= n; j++){ 78 cin>>v; 79 if(v){ 80 add_edge(i, j+MAXID); 81 } 82 } 83 } 84 int match = hungarian(); 85 if(match != n)cout<<-1<<endl; 86 else{ 87 for(int i = 1; i <= n; i++) 88 matching[i]-=100; 89 cnt = 0; 90 for(int i = 1; i <= n; i++){ 91 for(int j = 1; j <= n; j++){ 92 if(i == j)continue; 93 if(matching[j] == i){ 94 swap(matching[i], matching[j]); 95 a[cnt] = i; 96 b[cnt++] = j; 97 } 98 } 99 } 100 cout<<cnt<<endl; 101 for(int i = 0; i < cnt; i++) 102 cout<<"R "<<a[i]<<" "<<b[i]<<endl; 103 } 104 } 105 106 return 0; 107 }
标签:memset arch format can int class space continue script
原文地址:http://www.cnblogs.com/Penn000/p/7435303.html