vjudge可以交。
先给无向边定向,然后用网络流考虑反向以满足度数限制。
建图就像上下界网络流一样。
A C C o d e : \rm{AC Code:} ACCode:
#define maxn 105
#define maxm 5005
#define inf 0x3f3f3f3f
using namespace std;
int n,m,x[maxm],y[maxm],d[maxm],pos[maxm],in[maxn];
int info[maxn],Prev[maxm],to[maxm],cap[maxm],cnt_e=1;
void Node(int u,int v,int c){ Prev[++cnt_e]=info[u],info[u]=cnt_e,to[cnt_e]=v,cap[cnt_e]=c; }
void Node(int u,int v){ Prev[++cnt_e]=info[u],info[u]=cnt_e,to[cnt_e]=v; }
void Line(int u,int v,int c){ Node(u,v,c),Node(v,u,0); }
int sta[maxm];
void dfs(int u){
for(int &i=info[u],v;i;){
v = to[i] , i = Prev[i];
dfs(v);
}
sta[++sta[0]] = u;
}
int S,T,dis[maxn];
bool BFS(){
static int q[maxn],L,R;
L=R=0;
memset(dis,-1,sizeof dis);
dis[T] = 0 , q[R++] = T;
for(int u;L<R;){
u = q[L++];
for(int i=info[u];i;i=Prev[i])
if(cap[i^1] && dis[to[i]] == -1){
dis[to[i]] = dis[u] + 1;
q[R++] = to[i];
}
}
return dis[S] != -1;
}
int aug(int u,int mx){
if(u == T) return mx;
int st = mx , inc;
for(int i=info[u];i;i=Prev[i])
if(cap[i] && dis[to[i]]+1 == dis[u]){
inc = aug(to[i] , min(st , cap[i]));
st -= inc , cap[i] -= inc , cap[i^1] += inc;
if(!st) break;
}
return mx - st;
}
int main(){
// freopen("1.out","w",stdout);
int cas;
for(scanf("%d",&cas);cas--;){
memset(info,0,sizeof info),cnt_e=1;
memset(in,0,sizeof in);
scanf("%d%d",&n,&m);
S = n + 1 , T = S + 1;
char s[2];
for(int i=1,u,v;i<=m;i++){
scanf("%d%d%s",&u,&v,s);
x[i] = u , y[i] = v;
in[u]--,in[v]++;
if(s[0] == 'U'){
Line(v,u,1),pos[i]=cnt_e;
d[i] = 1;
}
else d[i] = -1;
}
bool ERR=0;
int stm = 0;
for(int i=1;i<=n;i++){
if(in[i] % 2) ERR = 1;
if(in[i] > 0) Line(S,i,in[i]/2) , stm += in[i] / 2;
else Line(i,T,-in[i]/2);
}
for(;BFS();)
stm -= aug(S,inf);
if(stm) ERR = 1;
else{
memset(info,0,sizeof info),cnt_e=1;
for(int i=1;i<=m;i++){
if(d[i] == 1 && cap[pos[i]])
swap(x[i],y[i]);
Node(x[i],y[i]);
}
sta[0] = 0;
dfs(1);
if(sta[0] < m) ERR = 1;
}
if(ERR) puts("No euler circuit exist");
else{
for(int i=sta[0];i>=1;i--) printf("%d%c",sta[i]," \n"[i==1]);
}
if(cas) puts("");
}
}