这题。。。按照拓扑序的顺序,从入度为0的点开始,当前入度为0的点的球向下转移,注意可能一个点可能同时转移到两个相同的点就好了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2005000;
/*struct E
{
ll to,next;
}e[maxn];*/
ll in[maxn],to[maxn],head[maxn],cnt=0;
struct node
{
ll s,l,r,f;
}a[maxn];
/*void add(int u,int v)
{
e[cnt].next=head[u];
e[cnt].to=v;
head[u]=cnt++;
}*/
ll n,m;
queue<ll>q;
void bfs(ll st)
{
for(ll i=1;i<=m;i++)
{
if(in[i]==0) q.push(i);
}
a[st].s=n;
while(!q.empty())
{
ll u=q.front();
q.pop();
//if(u==0) continue;
ll x=a[u].l,y=a[u].r;
if(a[u].f==0)
{
a[x].s+=a[u].s/2ll+a[u].s%2ll;
a[y].s+=a[u].s/2ll;
}
else
{
a[x].s+=a[u].s/2ll;
a[y].s+=a[u].s/2ll+a[u].s%2ll;
}
in[x]--;
if(in[x]==0&&x) q.push(x);
in[y]--;
//printf("u=%lld a[%lld].s=%lld a[%lld].s=%lld a[%lld].s=%lld\n",u,u,a[u].s,x,a[x].s,y,a[y].s);
if(in[y]==0&&y) q.push(y);
}
}
int main()
{
//memset(head,-1,sizeof(head));
scanf("%lld %lld",&n,&m);
for(ll i=1;i<=m;i++)
{
char op;
getchar();
scanf("%c %lld %lld",&op,&a[i].l,&a[i].r);
in[a[i].l]++;in[a[i].r]++;
//out[i]++;
//add(i,a[i].l);add(i,a[i].r);
if(op=='L') a[i].f=0;
else a[i].f=1;
}
/*for(ll i=1;i<=m;i++)
{
printf("a[%lld].l=%lld a[%lld].r=%lld a[%lld].f=%lld\n",i,a[i].l,i,a[i].r,i,a[i].f);
}*/
bfs(1);
for(ll i=1;i<=m;i++)
{
if((a[i].f+a[i].s)%2ll==0) printf("L");
else printf("R");
}
printf("\n");
}
/*
5 3
L 2 3
R 0 3
L 0 0
*/