国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hdoj 2819 Swap (最大匹配+輸出路徑)

2019-11-08 02:37:36
字體:
來源:轉載
供稿:網友

題意:

給你一個每位是0或1的矩陣,問你是否可以經過交換行和交換列使矩陣的對角線都為1,如果可以輸出交換的方法。

思路:

行和列的匹配,左邊是行,右邊是列,然后如果行列交點是1,那么就可以匹配,看是否為完美匹配,然后輸出怎么交換的。

輸出交換的路徑:

可以鎖定行,來選擇列和它匹配,將選擇的列移動到和該行形成對角線上是1的位置,比如和第一行匹配的列,就要移動要第一列,第二行的,就到第二列。其實就是對第i行,找一個第i個數是1的列和它匹配,然后看是否是最大匹配!

代碼:

#include<bits/stdc++.h>using namespace std;const int maxn = 105;vector<int> e[maxn];int n, match[maxn], vis[maxn], ans1[maxn], ans2[maxn];bool dfs(int x){    for(int i = 0; i < e[x].size(); i++)    {        int to = e[x][i];        if(!vis[to])        {            vis[to] = 1;            if(!match[to] || dfs(match[to]))            {                match[to] = x;                return true;            }        }    }    return false;}int Hungary(){    int res = 0;    for(int i = 1; i <= n; i++)    {        memset(vis, 0, sizeof(vis));        res += dfs(i);    }    return res;}int main(void){    while(cin >> n)    {        memset(match, 0, sizeof(match));        for(int i = 1; i < maxn; i++)            e[i].clear();        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++)            {                int t;                scanf("%d", &t);                if(t) e[i].push_back(j);            }        if(Hungary() != n) puts("-1");        else        {            int cnt = 0;            for(int i = 1; i <= n; i++)            {                int pos;                for(int j = 1; j <= n; j++)                    if(match[j] == i)                    {                        pos = j;                        break;                    }                if(pos != i)                {                    ans1[cnt] = i;                    ans2[cnt++] = pos;                    swap(match[i], match[pos]);                }            }            PRintf("%d/n", cnt);            for(int i = 0; i < cnt; i++)                printf("C %d %d/n", ans1[i], ans2[i]);        }    }    return 0;}

Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1?InputThere are several test cases in the input. The first line of each test case is an integer N (1 <= N <= 100). Then N lines follow, each contains N numbers (0 or 1), separating by space, indicating the N*N matrix.OutputFor each test case, the first line contain the number of swaps M. Then M lines follow, whose format is “R a b” or “C a b”, indicating swapping the row a and row b, or swapping the column a and column b. (1 <= a, b <= N). Any correct answer will be accepted, but M should be more than 1000. If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”. Sample Input
20 11 021 01 0Sample Output
1R 1 2-1
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枣阳市| 青河县| 宜兰市| 金昌市| 应城市| 九江县| 博客| 玉树县| 泊头市| 响水县| 石柱| 岱山县| 双桥区| 万宁市| 保山市| 连州市| 桂东县| 莱西市| 新巴尔虎左旗| 含山县| 清苑县| 许昌市| 观塘区| 晋江市| 达孜县| 宁河县| 灵山县| 额济纳旗| 四平市| 台州市| 古交市| 武川县| 报价| 贵州省| 基隆市| 林甸县| 太和县| 新津县| 汉中市| 秦皇岛市| 景谷|