hdu3729 I'm Telling the Truth

匹配模版題

看最大匹配數是否等於人數

要求輸出字典序最小   那麼反序匹配就好了


#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
#include <sstream>
using namespace std;

//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))

//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s)

typedef long long LL;
typedef vector <int> VI;

const int INF = 1000000007;
const double eps = 1e-10;
const int MOD = 1000000000;
const int maxn = 100000;

struct Node{
    int l, r;
}t[70];
int vis[100010];
int n;
vector<int>adj[400];
map<int, int> M;
int cnt;

bool used[maxn];   // mat[i][j]==1表示X[i]與Y[i]有連線,vis尋找增廣路徑時判斷點是否被訪問過
int mx[maxn],my[maxn];

int path(int s)   // 如果s能找到一個匹配點,返回1,否則返回0
{
     for(int i = 0; i < adj[s].size(); i++)   // 遍歷Y[],尋找X[s]的匹配
     {
         int e = adj[s][i];
         if(!used[e])
         {
             used[e]=1;
             if(my[e] == -1 || path(my[e]))
             {
                 my[e]=s;
                 mx[s]=e;
                 return 1;
             }
         }
     }
     return 0;
}

int MaxMatch()
{
     int res=0;
     CLR(mx,-1);
     CLR(my,-1);
     for(int i=n - 1; i>=0; i--)   // 遍歷X[],逐個尋找增廣路徑
     {
         CLR(used,0);
         res+=path(i);
     }
     return res;   // 返回最大匹配數
}

int main()
{
    int T;
    RI(T);
    while (T--)
    {
        RI(n);
        CLR(vis, 0);
        REP(i, 400) adj[i].clear();
        M.clear();
        REP(i, n)
        {
            RII(t[i].l, t[i].r);
            FE(j, t[i].l, t[i].r) vis[j]++;
        }
        cnt = n;
        REP(i, n)
        {
            FE(j, t[i].l, t[i].r)
            {
                if (M[j] == 0) M[j] = cnt++;
                adj[i].push_back(M[j]);
                if (vis[j] == 1) break;
            }
        }
        int ans = MaxMatch();
        printf("%d\n", ans);
        bool f = false;
        REP(i, n)
        if (mx[i] != -1)
        {
            if (f) putchar(' ');
                printf("%d", i +1);
            f = true;
        }
        puts("");
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章