试题编号: | 201903-4 |
试题名称: | 消息传递接口 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: | |
#include<bits/stdc++.h>
using namespace std;
struct node{
int flag,t;
node(string& s,int &n){
int temp=0,i=1;
if(s[0] == 'R'){
flag = 0;
}else{
flag = 1;
}
while(s[i]){
temp = 10*temp + (s[i++]-'0');
}
t = temp%n;
}
};
vector<queue<node> > v;
queue<node> q;
int main(){
int T,n;
cin >> T >> n;
getchar();
string str;
stringstream stream;
while(T--){
for(int i = 0;i < n;i++){
getline(cin,str);
stream << str;
while(stream>>str){
q.push(node{str,n});
}
v.push_back(q);
stream.clear();
while(!q.empty()){
q.pop();
}
}
int ins;
while(1){
ins = 0;
for(int i = 0;i < n;i++){
if(v[i].empty()) continue;
node a = v[i].front();
if(v[a.t].empty()) break;
node b = v[a.t].front();
if((a.flag+b.flag) != 1) continue;
if(b.t != i) continue;
v[i].pop();v[a.t].pop();
ins=1;
i--;
}
if(!ins) break;
}
for(int i = 0;i < n;i++){
if(!v[i].empty()) {
ins = 1;
cout << "1" << endl;
break;
}
}
if(ins==0){
cout << "0" << endl;
}
while(!v.empty()) v.pop_back();
}
return 0;
}