1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
ifstream in("drumuri.in");
ofstream out("drumuri.out");
unsigned int n,m;
//typedef vector<bool> row;
//typedef vector<row> matrix;
//matrix a(40000, row(40000));
int a[100][100];
int main();
void op1(unsigned int i, unsigned int j);
void op2(unsigned int i, unsigned int j);
void op3(unsigned int i, unsigned int d);
void op4(unsigned int i);
vector<int> capete(unsigned int i);
int main()
{
in>>n>>m;
unsigned int i,k,l;
int j;
for (i=1;i<=m;i++)
{
in>>j;
if(j4) in>>k>>l;
else in>>k;
if(j==1) op1(k1,l1);
if(j==2) op2(k1,l1);
if(j==3) op3(k1,l);
if(j==4) op4(k1);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++) cout<<a[i][j]<<" ";
cout<<endl;
}
cout.flush();
system("pause");
return 0;
}
vector<int> capete(unsigned int i)
{
vector<int> capat;
vector<unsigned int> v,v1,v2;
int nr=1;
unsigned int k;
for (k=0;k<n;k++)
if (a[i][k]==1) v.push_back(k);
if(v.size()==0) capat.push_back(i);
else
if(v.size()==1)
{
v1.push_back(i);
v1.push_back(v.at(0));
while (nr)
{
nr;
for(k=0;k<n;k++)
if(a[v1.at(v1.size()1)][k]==1&&k!=v1.at(v1.size()2))
{
v1.push_back(k);
nr++;
}
}
capat.push_back(min(i,v1.at(v1.size()1)));
capat.push_back(max(i,v1.at(v1.size()1)));
}
else
if(v.size()==2)
{
v1.push_back(i);
v1.push_back(v.at(0));
v2.push_back(i);
v2.push_back(v.at(1));
while (nr)
{
nr;
for(k=0;k<n;k++)
if(a[v1.at(v1.size()1)][k]==1&&k!=v1.at(v1.size()2))
{
v1.push_back(k);
nr++;
}
}
while (nr)
{
nr;
for(k=0;k<n;k++)
if(a[v2.at(v2.size()1)][k]==1&&k!=v2.at(v2.size()2))
{
v2.push_back(k);
nr++;
}
}
capat.push_back(min(v1.at(v1.size()1),v2.at(v2.size()1)));
capat.push_back(max(v1.at(v1.size()1),v2.at(v2.size()1)));
}
return capat;
}
void op1(unsigned int i, unsigned int j)
{
vector<int> capat;
int v1=0,v2=0;
unsigned int k;
for (k=0;k<n;k++)
{
if(a[i][k]==1)v1++;
if(a[j][k]==1)v2++;
}
if(v1>=2v2>=2)
{
out<<0<<endl;
return;
}
capat=capete(i);
for(k=0;k<capat.size();k++)
if(capat.at(k)==j)
{
out<<0<<endl;
return;
}
a[i][j]=1;
a[j][i]=1;
out<<1<<endl;
return;
}
void op2(unsigned int i, unsigned int j)
{
if(a[i][j]==0)
{
out<<0<<endl;
return;
}
a[i][j]=0;
a[j][i]=0;
out<<1<<endl;
return;
}
void op3(unsigned int i, unsigned int d)
{
if(d==0)
{
out<<1<<" "<<i+1<<endl;
return;
}
vector<unsigned int> v,v1,v2;
int nr=1;
unsigned int k;
for (k=0;k<n;k++)
if (a[i][k]==1) v.push_back(k);
if(v.size()==0) out<<0<<endl;
else
if(v.size()==1)
{
v1.push_back(i);
v1.push_back(v.at(0));
while (nr)
{
nr;
for(k=0;k<n;k++)
if(a[v1.at(v1.size()1)][k]==1&&k!=v1.at(v1.size()2))
{
v1.push_back(k);
nr++;
}
}
if (v1.at(d)) out<<1<<" "<<v1.at(d)+1<<endl;
else out<<0<<endl;
}
else
if(v.size()==2)
{
v1.push_back(i);
v1.push_back(v.at(0));
v2.push_back(i);
v2.push_back(v.at(1));
while (nr)
{
nr;
for(k=0;k<n;k++)
if(a[v1.at(v1.size()1)][k]==1&&k!=v1.at(v1.size()2))
{
v1.push_back(k);
nr++;
}
}
nr=1;
while (nr)
{
nr;
for(k=0;k<n;k++)
if(a[v2.at(v2.size()1)][k]==1&&k!=v2.at(v2.size()2))
{
v2.push_back(k);
nr++;
}
}
if (v1.at(d)&&v2.at(d)) out<<2<<" "<<min(v1.at(d),v2.at(d))+1<<" "<<max(v1.at(d),v2.at(d))+1<<endl;
else if(v1.at(d)) out<<1<<" "<<v1.at(d)+1<<endl;
else if(v2.at(d)) out<<1<<" "<<v2.at(d)+1<<endl;
else out<<0<<endl;
}
}
void op4(unsigned int i)
{
vector<int> capat;
capat=capete(i);
if (capat.size()==1) out<<1<<" "<<capat.at(capat.size()1)+1<<endl;
else if(capat.size()==2) out<<2<<" "<<min(capat.at(capat.size()2),capat.at(capat.size()1))+1<<" "<<max(capat.at(capat.size()2),capat.at(capat.size()1))+1<<endl;
}
}
 