Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

怎么不A,帮帮我吧!

Posted by yc5_yc at 2012-08-04 19:22:12 on Problem 1043
#include <cstdio>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
const int NMax=30;
int N,L1,L2,connect[NMax];
bool ok[NMax],sta[NMax];
map<string,int> ID1,ID2;
map<int,string> rev1,rev2;
map<int,bool> State;
bool G[NMax][NMax];
bool DFS(int a) {
    sta[a]=1;
    for(int j=1;j<=N;j++) if(G[a][j]){
        if(!connect[j]) {
            connect[j]=a;
            ok[a]=1;
            return 1;
        }else if(!sta[connect[j]] && DFS(connect[j])) {
            ok[a]=1;
            connect[j]=a;
            return 1;
        }
    }
    return 0;
}
bool cmp(int a,int b) {
    return rev1[a]<rev1[b];
}
int main()
{
    string tmp;
    char buf[NMax],buf2[NMax];
    scanf("%d",&N);
    for(int i=1;i<=N;i++) {
        scanf("%s",buf);
        int len=strlen(buf);tmp.clear();
        for(int j=0;j<len;j++) tmp.push_back(buf[j]);
        ID2[tmp]=++L2;rev2[L2]=tmp;    
    }
    while(scanf("%s",buf),(buf[0]!='Q')) {
        scanf("%s",buf2);
        int len=strlen(buf2);tmp.clear();
        for(int i=0;i<len;i++) tmp.push_back(buf2[i]);
        if(buf[0]=='M') {
            int ID=ID2[tmp];
            int Maxn=0;
            for(map<int,bool>::iterator p=State.begin();p!=State.end();p++) if(p->second) { 
                G[ID][p->first]=1;
                if(p->first>Maxn) Maxn=p->first;
            }
            for(int i=Maxn+1;i<=N;i++) G[ID][i]=0;
            continue;
        }
        if(ID1[tmp]==0) {ID1[tmp]=++L1;rev1[L1]=tmp;}
        int ID=ID1[tmp];
        if(buf[0]=='E') State[ID]=1;
        else if(buf[0]=='L') State[ID]=0;
    }
    //puts("dsdfsdf");
    for(int i=1;i<=N;i++) connect[i]=0;
    for(int i=1;i<=N;i++) ok[i]=0;
    for(int i=1;i<=N;i++) {
        for(int j=1;j<=N;j++) if(G[i][j]){
            if(!connect[j]) {
                connect[j]=i;
                ok[i]=1;
                break;
            }
        }
    }
    for(int i=1;i<=N;i++) {
        for(int j=1;j<=N;j++) sta[j]=0;
        if(!ok[i]) DFS(i);
    }
    int R[NMax];
    for(int i=1;i<=N;i++) R[i]=i;
    sort(R+1,R+N+1,cmp);
    for(int i=1;i<=N;i++) {
        if(connect[R[i]]) cout <<rev1[R[i]]<<':'<<rev2[connect[R[i]]]<<endl;
        else cout <<rev1[R[i]]<<':'<<"???"<<endl;
    }
    getchar(); getchar();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator