[POI2011] SMI-Garbage problem solution

Face questions

Gangster surely we must think of the present and the target value while adding to the inconsistency of a new map;

The problem becomes a search for a new map in the number of Euler, and outputs these paths;

We can use the stack to record the case, then the answer would be for a little processing just fine;

#include <bits/stdc++.h>
#define inc(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
struct littlestar{
    int to;
    int nxt;
}star[2500010];
int head[2500010],cnt=1;
void add(int u,int v)
{
    star[++cnt].to=v;
    star[cnt].nxt=head[u];
    head[u]=cnt;
} 
int n,m;
int du[2500010];
int st[2500010],top;
int vis[2500010];
int ans;
void dfs(int u,int goal)
{
    st[++top]=u;
    du[u]--;
    du[u]--;
    for(int i=head[u];i;i=star[i].nxt){
        int v=star[i].to;
        head[u]=i;
        if(vis[i]) continue;
        vis[i]=vis[i^1]=1;
        if(u!=goal&&v==goal){
            ++ans;
            st[++top]=v;
            return;
        }
        dfs(v,goal);
        return ;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    inc(i,1,m){
        int u,v,w,goal;
        scanf("%d%d%d%d",&u,&v,&w,&goal);
        if(w==goal) continue;
        add(u,v);
        add(v,u);
        du[u]++;
        du[v]++;
    }
    inc(i,1,n){
        if(du[i]&1){
            cout<<"NIE"<<endl;
            return 0;
        }
    }
    inc(i,1,n){
        while(du[i]){
            dfs(i,i);
        }
    }
    cout<<ans<<endl;
    inc(i,1,top){
        int goal=st[i];
        ++i;
        int num=0;
        while(st[i]!=goal&&i<=top) ++i,++num;
        cout<<num+1<<" ";
        inc(j,i-num,i){
            printf("%d ",st[j]);
        }
        printf("%d",st[i-num]);
        cout<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11585325.html