hdu3849:求桥

Orz第一次写map,规则如下:

1.map<int /...A,int/...B>mp;

2.mp.find(A)==mp.end(),A还没有匹配;

3.mp[A]=B;

eg:

struct node{
     char c[25];
     bool operator<(const node&rhs) const{
          return strcmp(c,rhs.c)<0;}
};

map<node,int>mp;

不得已只能写vector了。。。

-----------------------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<map>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define adde(u,v) add(u,v),add(v,u)
const int nmax=10005;
int read(){
    int x=0,f=1;
    char c=getchar();
    while(!isdigit(c)){
        if(c=='-') f=-1;
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
struct node{
    char c[25];
    bool operator<(const node&rhs) const{
      return strcmp(c,rhs.c)<0;}
};
struct edge{
    node u,v;
    bool f;
};
edge e[1000000];
int n,m,cur,pre[nmax],low[nmax],dfn_clock;
map<node,int>mp;
vector<int>g[10005];
void dfs(int x,int fa){
    low[x]=pre[x]=++dfn_clock;
    for(int i=0;i<g[x].size();i++){
        int to=g[x][i];
        if(!pre[to]){
            dfs(to,x);
            low[x]=min(low[x],low[to]);
        }else if(pre[to]<pre[x]&&to!=fa)
            low[x]=min(low[x],pre[to]);
    }
    return ;
}
void init(){
    cur=0;clr(pre,0);rep(i,n) g[i].clear();dfn_clock=0;mp.clear();
}
int main(){
    int t=read();
    while(t--){
        n=read(),m=read();
        init();
        rep(i,m){
            scanf("%s%s",e[i].u.c,e[i].v.c);
            e[i].f=false;
            if(mp.find(e[i].u)==mp.end()) mp[e[i].u]=++cur;
            if(mp.find(e[i].v)==mp.end()) mp[e[i].v]=++cur;
            int u=mp[e[i].u],v=mp[e[i].v];
            g[u].push_back(v);g[v].push_back(u);
        }
        dfs(1,-1);
        bool ok=false;
        rep(i,n) if(!pre[i]) ok=true;
        if(ok) printf("0\n");
        //puts(0);cz大爷说应该是puts(“0”); 
        else{
            int ans=0;
            rep(i,m){
                int u=mp[e[i].u],v=mp[e[i].v];
                if(low[u]>pre[v]||low[v]>pre[u]) ans++,e[i].f=true;
            }
            printf("%d\n",ans);
            rep(i,m){
                if(e[i].f) printf("%s %s\n",e[i].u.c,e[i].v.c);
            }
        }
    }
    return 0;
}

-----------------------------------------------------------------------------------------------------------

你可能感兴趣的:(hdu3849:求桥)