Code: Select all
1
5
c 1 3
c 3 4
c 4 5
q 1 2
q 3 4
q 1 5
q 3 5
Moderator: Board moderators
Code: Select all
1
5
c 1 3
c 3 4
c 4 5
q 1 2
q 3 4
q 1 5
q 3 5
Code: Select all
m=p[x];
n=p[y];
p[y]=n; //No meaning, I think it should be p[x]=n
Code: Select all
#include<iostream>
using namespace std;
///////////////////////////////////////////////////////////////////////////////
typedef struct _set
{
int v,t;
}PG;
PG s[1001];
void makeset(PG* s,int smax)
{
for(int i=1;i<=smax;i++){s[i].v=i;s[i].t=i;}
}
int find(PG *s,int a)
{
while(s[a].t!=a)a=s[a].t;
return a;
}
void merge(PG *s,int a,int b)
{
a=find(s,a);b=find(s,b);
if(a<b)s[b].t=a;
else s[a].t=b;
}
bool same(PG *s,int a,int b)
{
if(find(s,a)==find(s,b))return true;
else return false;
}
void print_s(PG *s,int smax)
{
for(int i=1;i<=smax;i++)cout<<s[i].v<<" "<<s[i].t<<endl;
}
///////////////////////////////////////////////////////////////////////////////
int main()
{
int qmax,cmax,y,n;
int t1,t2;
char ch;
cin>>qmax;
for(int qi=1;qi<=qmax;qi++)
{
y=n=0;
cin>>cmax;
makeset(s,cmax);
cin.get();
while(1)
{
ch=cin.peek();
if(ch=='\n')break;
cin.get();
cin>>t1>>t2;
if(ch=='c')
merge(s,t1,t2);
if(ch=='q')
{
if(same(s,t1,t2))y++;
else n++;
}
cin.get();
}
cout<<y<<","<<n<<endl<<endl;
}
}
Code: Select all
I have got AC!
Code: Select all
removed
Only about 2 hours have passed between your 2 posts. Think, you are too optimistic.asif_rahman0 wrote:still no reply:(
Read this - http://acm.uva.es/problemset/minput.htmlasif_rahman0 wrote:can someone tell me about multiple I/O??
I don't think so.asif_rahman0 wrote:i m getting TLE because of this thing.
Code: Select all
scanf("%d\n",&T);
while(T--){
scanf("%d\n",&n);
rank=malloc((n+1)*sizeof *rank);
p=malloc((n+1)*sizeof *p);
for(i=1;i<=n;i++)
make_set(i);
n1=n2=0;
while(gets(s)!=NULL && sscanf(s,"%c %d %d",&ch,&i,&j)==3){
/*blah-blah/*
}feof(stdin))
break;
}