#include <iostream>
#include <string>
#include <map>
#include <cstdio>
using namespace std;
const int N=10010;
int father[N],relation[N],k=1;
void init(){//初始化操作
for(int i=0;i<N;++i){
father[i]=i;
relation[i]=0;
}
}
int min(int x,int y){
return x<y?x:y;
}
int max(int x,int y){
return x>y?x:y;
}
int find(int x){
int fx=father[x];
if(x!=father[x]){
father[x]=find(father[x]);
relation[x]=(relation[fx]+relation[x])%2;
}
return father[x];
}
bool Union_Set(int x,int y,int d){
int rootx=find(x);
int rooty=find(y);
if(rootx!=rooty){
father[rooty]=rootx;
relation[rooty]=(relation[x]+d-relation[y]+2)%2;
return true;
}
else{
if((relation[x]+relation[y])%2==d)
return true;
return false;
}
}
int main(){
int len,n;
scanf("%d%d",&len,&n);
init();
map<int,int> mp;
int x,y,a,b;
string ss;
bool flag=true;
int value=n;
for(int i=1;i<=n;++i){
scanf("%d%d",&a,&b);
cin>>ss;
x=min(a,b);//由于x可能比y大
y=max(a,b);
if(!flag){//已经确定是哪个错误,不需要执行下面的操作,题目中要求
continue;
}
x--;
if(mp.find(x)==mp.end()){//离散化操作
{mp[x]=k++;}
}
int mx=mp[x];
if(mp.find(y)==mp.end()){
{mp[y]=k++;}
}
int my=mp[y];
int d;
if(ss=="even")d=0;
else d=1;
flag=Union_Set(mx,my,d);
if(flag==false){
value=i-1;
}
}
printf("%d\n",value);
return 0;
}
#include <iostream>
#include <cstdio>
#include <string.h>
const int N=1000005;
int n,m,father[2*N],mark[2*N],k=0;
bool flag[N*2];
void init(){//初始化
k=n+1;
for(int i=1;i<2*N;++i){
father[i]=i;
mark[i]=i;
}
}
int find(int x){//寻找父节点
if(x==father[x])return x;
else father[x]=find(father[x]);
}
void union_set(int x,int y){
x=mark[x];
y=mark[y];
int rootx=find(x);
int rooty=find(y);
father[rootx]=rooty;
}
void del(int x){
mark[x]=k;
father[k]=k;
k++;
}
int main(){
int kk=1;
while(scanf("%d%d",&n,&m)){
if(n+m==0)break;
init();
char ch[2];
int x,y;
while(m--){
scanf("%s",ch);
if(ch[0]=='M'){//合并操作
scanf("%d%d",&x,&y);
union_set(x+1,y+1);//因为x y可能为0,故加1
}
else{
scanf("%d",&x);
del(x+1);//删除结点的操作,即删除该结点的所有关系
}
}
int sum=0;
//统计最后有多少个集合
memset(flag,false,sizeof(flag));
for(int i=1;i<=n;++i){
int marki=mark[i];
int rooti=find(marki);
if(!flag[rooti]){
sum++;
flag[rooti]=true;
}
}
printf("Case #%d: %d\n",kk++,sum);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int N=10000005;
int maxx,father[N],rankk[N];
void init(int x){//初始化操作
for(int i=0;i<N;++i)
{father[i]=i;rankk[i]=1;}
}
int find(int x){//寻找父节点
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void unionset(int a,int b){//合并操作
int x=find(a);
int y=find(b);
if(x==y)
return;
if(rankk[x]>=rankk[y]){//按秩合并
father[y]=x;
rankk[x]+=rankk[y];
if(rankk[x]>maxx)
maxx=rankk[x];
}
else{
father[x]=y;
rankk[y]+=rankk[x];
if(rankk[y]>maxx)
maxx=rankk[y];
}
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==0)
{ printf("1\n");continue;}
init(n);
maxx=0;
int a,b;
while(n--){
scanf("%d%d",&a,&b);
unionset(a,b);
}
printf("%d\n",maxx);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <malloc.h>
using namespace std;
struct Tire{
int count;
Tire *tire[26];
}*a;
void init(){
a=(Tire *)malloc(sizeof(Tire));
for(int i=0;i<26;++i)
a->tire[i]=NULL;
}
void insert(char ch[]){
int len=strlen(ch);
Tire *head=a;
for(int i=0;i<len;++i){
int k=ch[i]-'a';
if(head->tire[k]!=NULL){
head=head->tire[k];
head->count++;
}
else{
head->tire[k]=new Tire;
head=head->tire[k];
head->count=1;
for(int j=0;j<26;++j)
head->tire[j]=NULL;
}
}
}
int find(char ch[]){
int len=strlen(ch);
Tire *head=a;
int i,k;
for(i=0;i<len;++i){
k=ch[i]-'a';
if(head->tire[k]==NULL){
return 0;
}
else{
head=head->tire[k];
}
}
return head->count;
}
int main(){
char s[10],ss[10];
init();
int len,num=0;
while(gets(s)){
len=strlen(s);
if(len==0)
break;
insert(s);//先输入字符串
}
while(gets(ss)){
num=find(ss);//寻找以字符串ss为前缀的单词数量
printf("%d\n",num);
}
return 0;
}