QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123121 | #6413. Classical Graph Theory Problem | ExplodingKonjac | WA | 55ms | 27316kb | C++17 | 6.7kb | 2023-07-11 19:18:18 | 2023-07-11 19:18:20 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define OPENIOBUF
namespace FastIO
{
class FastIOBase
{
protected:
#ifdef OPENIOBUF
static const int BUFSIZE=1<<16;
char buf[BUFSIZE+1];
int buf_p=0;
#endif
FILE *target;
FastIOBase(FILE *f): target(f){}
~FastIOBase()=default;
public:
#ifdef OPENIOBUF
virtual void flush()=0;
#endif
};
class FastOutput final: public FastIOBase
{
#ifdef OPENIOBUF
public:
void flush()
{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
private:
void __putc(char x)
{
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
putc(x,target);
#endif
}
template<typename T>
void __write(T x)
{
char stk[64],*top=stk;
if(x<0) return __putc('-'),__write(-x);
do *(top++)=x%10,x/=10; while(x);
for(;top!=stk;__putc(*(--top)+'0'));
}
public:
FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
~FastOutput(){ flush(); }
#endif
FastOutput &operator <<(char x)
{ return __putc(x),*this; }
FastOutput &operator <<(const char *s)
{ for(;*s;__putc(*(s++)));return *this; }
FastOutput &operator <<(const string &s)
{ return (*this)<<s.c_str(); }
template<typename T>
enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
{ return __write(x),*this; }
template<typename ...T>
void writesp(const T &...x)
{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
template<typename ...T>
void writeln(const T &...x)
{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
template<typename Iter>
void writesp(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<' '; }
template<typename Iter>
void writeln(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;
class FastInput final: public FastIOBase
{
#ifdef OPENIOBUF
public:
void flush()
{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
private:
bool __eof;
char __getc()
{
if(__eof) return EOF;
#ifdef OPENIOBUF
if(buf_p==BUFSIZE) flush();
char ch=buf[buf_p++];
#else
char ch=getc(target);
#endif
return ~ch?ch:(__eof=true,EOF);
}
void __ungetc(char c)
{
__eof=false;
#ifdef OPENIOBUF
buf_p--;
#else
ungetc(c,target);
#endif
}
public:
FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{ buf_p=BUFSIZE; }
bool eof()const { return buf[buf_p]==EOF; }
#else
{}
bool eof()const { return feof(target); }
#endif
char peek() { return __getc(); }
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=__getc()));return *this; }
template<typename T>
enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
{
char ch,sym=0;x=0;
while(isspace(ch=__getc()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=__getc();
for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
return __ungetc(ch),sym?x=-x:x,*this;
}
FastInput &operator >>(char *s)
{
while(isspace(*s=__getc()));
if(__eof) return *this;
for(;!isspace(*s) && !__eof;*(++s)=__getc());
return __ungetc(*s),*s='\0',*this;
}
FastInput &operator >>(string &s)
{
char str_buf[(1<<8)+1]={0},*p=str_buf;
char *const buf_end=str_buf+(1<<8);
while(isspace(*p=__getc()));
if(__eof) return *this;
for(s.clear(),p++;;p=str_buf)
{
for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
if(p!=buf_end) break;
s.append(str_buf);
}
__ungetc(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &...x)
{ initializer_list<int>{(this->operator>>(x),0)...}; }
template<typename Iter>
void read(Iter begin,Iter end)
{ while(begin!=end) (*this)>>*(begin++); }
}qin;
} // namespace FastIO
using FastIO::qin,FastIO::qout;
using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;
#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#else
#define FILEIO(file)
#endif
int T,n,m,deg[200005],lef[200005],ans[200005];
struct Edge{ int u,v; }e[500005];
bool del[500005];
vector<pair<int,int>> g1[200005],g2[200005];
int fa[200005],sum[200005],tag[200005];
inline int findFa(int x)
{ return x!=fa[x]?fa[x]=findFa(fa[x]):x; }
inline void merge(int x,int y)
{ x=findFa(x),y=findFa(y);if(x!=y)fa[y]=x,sum[x]+=sum[y]; }
#undef assert
#define assert(cond) ((cond)||(cout<<"Assertion "#cond" failed.\n",exit(0),0))
int main()
{
qin>>T;
while(T--)
{
qin>>n>>m;
for(int i=1;i<=m;i++)
{
qin>>e[i].u>>e[i].v;
deg[e[i].u]++;
deg[e[i].v]++;
}
for(int i=1;i<=m;i++)
{
auto &[u,v]=e[i];
if(deg[u]==1) lef[v]++;
if(deg[v]==1) lef[u]++;
g1[u].emplace_back(v,i);
g1[v].emplace_back(u,i);
}
for(int i=1;i<=m;i++)
{
auto [u,v]=e[i];
if(deg[u]==1 || deg[v]==1) continue;
bool fl=true;
del[i]=true;
if(deg[u]==2) for(auto &[w,j]: g1[u])
if(!del[j]) fl&=((++lef[w])<=2);
if(deg[v]==2) for(auto &[w,j]: g1[v])
if(!del[j]) fl&=((++lef[w])<=2);
if(fl) deg[u]--,deg[v]--;
else
{
if(deg[u]==2) for(auto &[w,j]: g1[u])
if(!del[j]) lef[w]--;
if(deg[v]==2) for(auto &[w,j]: g1[v])
if(!del[j]) lef[w]--;
del[i]=false;
}
}
iota(fa+1,fa+n+1,1);
fill(tag+1,tag+n+1,1);
for(int i=1;i<=n;i++)
{
if(deg[i]==1)
{
if(!lef[i] || ans[i]) continue;
int u=i,v=0;
for(auto &[w,j]: g1[i]) if(!del[j]) v=w;
ans[u]=sum[u]=1;
ans[v]=sum[v]=-1;
merge(u,v);
}
else if(!lef[i])
{
int u=0,v=0;
for(auto &[w,j]: g1[i]) if(!del[j]) (u?v:u)=w;
assert(lef[u] && lef[v]);
if(u>v) swap(u,v);
g2[v].emplace_back(u,i);
}
}
for(int u=1;u<=n;u++)
{
if(deg[u]==1 || !lef[u]) continue;
int c[2]={0,0};
for(auto &[v,w]: g2[u]) merge(u,v),merge(u,w),c[ans[v]>0]++;
if(c[1]!=c[0]) ans[u]=(c[1]<c[0]?1:-1);
else ans[u]=(abs(sum[u]-1-c[0])<abs(sum[u]+1+c[1])?1:-1);
sum[u]+=ans[u];
for(auto &[v,w]: g1[u])
if(!del[w] && deg[v]==1)
merge(u,v),sum[u]+=(ans[v]=-ans[u]);
for(auto &[v,w]: g2[u])
if(ans[u]==ans[v])
sum[u]+=(ans[w]=-ans[u]);
for(auto &[v,w]: g2[u])
if(ans[u]!=ans[v])
sum[u]+=(ans[w]=(sum[u]>0?-1:1));
assert(abs(sum[u])<=1);
}
int tmp=0;
for(int i=1;i<=n;i++)
{
if(findFa(i)!=i) continue;
if(abs(tmp+sum[i])>abs(tmp-sum[i]))
tag[i]=-1,sum[i]=-sum[i];
tmp+=sum[i];
}
assert(abs(tmp)<=1);
tag[0]=(tmp>0?-1:1);
for(int i=1;i<=n;i++)
if(ans[i]*tag[findFa(i)]*tag[0]>0)
qout<<i<<' ';
qout<<'\n';
// do cleaning
fill(del+1,del+m+1,false);
tag[0]=0;
for(int i=1;i<=n;i++)
{
deg[i]=lef[i]=sum[i]=ans[i]=0;
g1[i].clear(),g2[i].clear();
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 17732kb
input:
2 6 7 1 2 1 3 2 3 3 4 4 5 4 6 5 6 3 2 1 2 2 3
output:
1 2 6 2
result:
ok ok (2 test cases)
Test #2:
score: 0
Accepted
time: 21ms
memory: 15860kb
input:
10000 2 1 1 2 29 28 13 19 16 5 21 7 22 10 10 2 1 18 27 13 10 3 11 23 12 22 11 7 7 17 29 17 9 1 28 21 2 18 13 9 4 25 20 16 5 14 20 7 14 4 12 8 8 24 17 19 15 1 11 6 26 9 13 12 13 9 12 2 6 12 9 11 5 2 8 10 6 10 3 10 7 1 7 5 8 9 4 1 12 11 10 6 2 8 12 4 5 10 11 1 3 1 10 1 12 9 9 1 8 3 7 1 35 35 13 8 34 1...
output:
1 10 11 14 15 18 19 20 22 24 25 26 27 28 29 4 5 10 11 12 13 5 6 7 8 11 12 3 5 9 13 15 18 23 24 26 28 29 30 31 32 33 34 35 1 2 3 6 7 11 16 17 18 2 3 4 1 2 5 6 8 10 11 12 15 16 17 18 19 23 25 26 29 30 32 34 36 38 39 41 49 2 3 6 8 9 10 11 14 16 17 19 20 24 25 27 31 32 34 1 2 4 7 8 9 13 5 9 1...
result:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 17ms
memory: 17980kb
input:
1000 337 338 164 11 138 75 114 262 170 298 166 241 269 24 9 134 233 60 50 222 231 253 296 242 173 18 93 223 116 151 312 150 82 236 180 20 297 184 268 70 334 162 217 135 258 321 80 209 212 208 18 163 227 104 334 135 77 118 17 230 307 105 307 335 29 24 111 177 324 24 85 3 214 191 310 182 22 171 202 21...
output:
2 11 19 23 28 29 31 33 36 37 38 40 41 43 46 57 62 64 69 71 76 79 82 85 87 93 94 97 98 99 101 102 108 114 115 118 119 122 125 126 128 129 131 132 133 136 137 142 146 147 148 151 152 153 158 161 162 165 167 169 172 173 176 177 180 181 183 185 186 187 188 191 192 196 201 203 204 205 206 208 209 210 211...
result:
ok ok (1000 test cases)
Test #4:
score: 0
Accepted
time: 17ms
memory: 18688kb
input:
100 1038 1044 206 546 372 853 526 57 777 72 645 866 15 716 254 707 366 753 635 809 850 407 616 149 839 175 320 770 649 686 857 798 1027 40 988 566 315 500 187 615 100 523 867 708 51 381 858 9 177 55 310 54 355 215 78 26 740 570 523 797 828 693 930 981 208 185 663 957 298 523 235 496 622 174 285 247 ...
output:
1 3 5 6 7 8 9 10 14 15 16 17 19 20 21 22 23 24 25 26 27 28 29 30 31 32 34 35 37 38 39 40 41 43 45 46 47 48 50 51 54 55 57 59 60 61 62 63 64 66 67 70 71 74 76 77 78 79 80 81 82 83 85 86 87 88 90 91 92 93 94 95 97 98 99 102 105 108 109 110 111 112 116 118 119 121 122 125 126 127 128 129 130 131 132 13...
result:
ok ok (100 test cases)
Test #5:
score: 0
Accepted
time: 27ms
memory: 22372kb
input:
10 1380 1393 960 647 1319 708 57 1128 751 148 1291 602 835 921 942 406 622 616 967 91 555 545 871 10 447 471 1140 306 149 121 587 165 1179 936 256 787 332 374 729 129 631 481 976 86 1128 1300 477 776 460 313 538 632 1210 275 355 470 1324 885 870 1325 389 979 468 532 41 416 1026 243 1153 152 948 323 ...
output:
1 2 3 5 6 8 10 12 14 15 16 17 19 20 21 22 24 25 26 27 28 29 30 31 32 33 36 37 38 39 41 42 43 46 47 49 51 52 53 54 55 56 57 59 61 62 64 67 68 69 70 71 72 73 74 75 77 79 80 81 82 83 84 85 86 87 91 92 93 94 95 96 97 98 100 101 102 103 104 106 107 108 109 111 112 114 116 117 120 121 123 124 125 126 128 ...
result:
ok ok (10 test cases)
Test #6:
score: 0
Accepted
time: 55ms
memory: 27316kb
input:
1 200000 201978 69113 28513 94227 164392 56849 195513 22579 149089 195084 193248 121765 162768 135432 101508 107443 89723 12337 87598 173450 107835 13160 161882 18965 179808 53739 23609 114567 23456 195251 178048 61586 87664 179364 25594 90158 169714 30104 161354 143346 4279 177208 87389 122480 1269...
output:
2 3 4 6 7 8 9 10 11 12 15 17 18 19 20 21 22 24 25 26 27 30 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 53 54 55 56 57 59 60 61 62 63 64 65 66 67 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 88 91 92 93 94 95 96 97 98 99 100 101 102 103 104 106 108 109 110 111 113 114 115 117 118 119 ...
result:
ok ok (1 test case)
Test #7:
score: 0
Accepted
time: 23ms
memory: 17872kb
input:
10000 41 44 18 29 38 6 7 4 34 27 40 37 12 40 18 38 11 18 30 39 2 21 10 34 33 2 8 12 30 23 6 2 12 21 15 7 17 1 36 15 31 36 15 21 38 31 1 11 4 30 16 33 19 32 21 30 32 35 1 3 27 9 1 34 11 5 26 25 22 5 34 24 23 32 28 2 20 33 13 15 31 21 38 41 26 3 13 14 14 33 11 11 3 1 9 11 6 3 8 1 7 2 4 3 10 2 9 2 5 4 ...
output:
2 11 12 14 15 16 17 19 20 21 22 26 27 29 30 34 35 36 40 41 1 2 4 6 11 2 3 4 5 7 8 12 15 2 3 4 5 6 7 8 9 10 13 15 16 18 21 25 26 31 1 2 3 4 2 5 5 6 8 10 11 13 16 17 2 3 7 8 1 4 5 6 7 8 12 1 2 3 5 2 4 6 1 2 3 5 1 4 5 6 4 9 10 11 15 16 18 20 23 24 25 27 28 29 4 5 10 14 15 16 18 20 21 22 ...
result:
ok ok (10000 test cases)
Test #8:
score: 0
Accepted
time: 20ms
memory: 15732kb
input:
10000 11 13 6 3 9 4 10 4 9 6 10 7 1 5 2 11 2 8 10 6 2 9 6 7 2 5 5 11 3 2 2 1 2 3 2 1 2 1 12 14 12 11 10 7 5 6 2 5 5 8 8 3 8 1 3 12 12 7 2 10 10 11 6 4 11 2 9 3 4 4 1 2 1 3 4 3 2 3 11 13 3 7 1 5 1 6 8 5 9 7 1 2 1 11 2 4 10 9 10 1 7 2 8 3 8 6 2 1 1 2 15 18 3 11 2 10 7 14 14 4 7 3 6 11 15 12 5 11 2 7 7...
output:
1 2 6 10 11 2 1 1 3 4 5 7 11 1 3 1 4 7 8 10 1 3 6 7 8 12 14 15 3 5 6 7 8 9 11 12 16 17 18 19 22 24 27 29 30 31 32 35 36 37 42 45 47 48 49 52 54 57 58 60 64 4 5 6 10 11 13 2 6 7 8 2 8 11 12 15 16 20 22 23 24 25 27 31 32 34 35 37 38 39 4 5 7 13 15 17 22 26 27 33 34 37 38 43 44 47 48 52 53 ...
result:
ok ok (10000 test cases)
Test #9:
score: 0
Accepted
time: 18ms
memory: 18028kb
input:
10000 10 14 4 9 5 10 1 10 7 6 8 6 9 6 8 3 8 7 4 6 5 3 10 4 10 2 4 8 1 9 6 8 1 2 5 2 5 1 3 4 5 3 6 5 2 3 3 1 3 3 2 1 3 2 3 1 18 26 18 3 10 11 2 4 17 4 8 12 14 15 1 12 13 12 15 7 13 15 14 2 17 5 1 13 11 16 9 3 13 9 6 12 11 14 3 4 3 11 7 11 8 2 8 4 15 6 12 10 12 18 24 35 18 4 22 10 1 21 22 6 23 7 6 14 ...
output:
1 2 3 4 7 2 4 5 3 2 3 5 6 8 10 11 13 18 1 2 5 6 7 9 10 13 17 18 19 21 1 2 4 5 6 7 8 9 10 11 13 14 15 17 18 19 20 21 22 23 25 27 28 30 35 41 45 47 50 55 58 64 66 2 4 5 7 8 12 3 5 11 14 17 18 19 20 21 22 24 26 28 30 31 32 3 4 7 8 4 8 9 10 12 13 4 5 6 10 12 14 17 18 19 20 22 23 24 25 26 27 29...
result:
ok ok (10000 test cases)
Test #10:
score: 0
Accepted
time: 30ms
memory: 15956kb
input:
10000 4 6 1 3 2 3 4 2 4 1 1 2 4 3 25 51 19 15 19 10 12 3 9 7 5 4 7 21 25 12 20 16 1 13 20 14 15 12 20 13 8 5 16 9 17 13 3 25 25 20 16 22 4 8 5 7 9 10 5 11 4 24 13 21 9 4 15 24 16 11 13 4 22 21 4 14 20 10 12 6 1 4 3 18 9 6 5 2 24 3 16 4 6 16 25 16 21 16 22 25 3 21 10 15 25 23 1 19 7 15 15 20 19 14 17...
output:
1 3 3 5 8 9 15 16 17 19 20 21 22 25 4 5 1 3 4 6 7 8 11 12 19 25 26 27 28 29 30 1 3 4 7 8 10 11 13 14 18 20 21 24 26 27 29 31 36 1 4 5 6 8 9 11 18 19 2 3 7 9 11 13 14 15 18 19 20 21 22 26 28 29 1 2 4 7 8 12 2 3 5 6 10 11 13 5 7 12 15 19 20 25 26 28 29 30 32 34 35 36 37 38 39 40 41 1 2 3 4 5...
result:
ok ok (10000 test cases)
Test #11:
score: 0
Accepted
time: 35ms
memory: 17876kb
input:
10000 24 60 13 22 6 12 21 17 24 8 15 11 18 19 17 7 24 1 18 16 21 2 17 12 21 11 10 7 9 18 6 21 17 10 3 24 16 12 7 23 11 8 22 24 3 17 23 3 1 12 8 5 4 24 15 13 8 22 2 8 13 17 10 2 2 7 7 18 18 14 22 20 13 6 5 16 22 23 21 22 5 24 21 14 1 7 12 20 24 20 8 14 17 11 1 19 17 8 9 10 1 11 14 13 10 15 19 11 14 2...
output:
1 2 3 4 6 8 10 12 14 16 18 21 3 4 1 4 7 11 14 17 18 20 22 24 25 27 29 30 31 1 2 3 4 7 8 9 10 17 19 21 2 4 6 7 9 10 11 13 14 15 18 22 24 25 26 27 29 32 33 38 40 43 1 2 3 5 8 9 11 13 14 16 17 23 4 7 8 12 14 19 21 22 25 27 30 31 32 33 35 37 38 39 40 41 2 4 6 12 15 18 19 20 21 24 25 26 27 28 1 3...
result:
ok ok (10000 test cases)
Test #12:
score: 0
Accepted
time: 14ms
memory: 18100kb
input:
1000 53 57 47 22 30 20 37 51 19 4 39 22 29 53 1 11 53 18 33 52 29 2 21 50 42 50 42 49 36 44 37 16 5 24 52 35 8 36 28 29 9 24 24 34 32 37 44 46 31 2 13 45 5 21 3 19 17 47 14 35 33 43 43 27 48 13 16 12 33 30 26 14 8 49 41 27 43 45 6 9 36 22 20 37 38 5 17 25 3 7 42 3 33 10 23 50 1 14 40 24 45 42 48 52 ...
output:
2 5 7 9 10 11 13 14 16 18 19 22 25 29 30 34 37 40 41 45 46 47 49 50 52 53 2 9 11 13 15 16 20 22 30 32 34 36 38 40 41 42 43 44 45 46 47 49 51 52 53 55 56 57 59 1 2 4 5 6 8 9 10 13 14 15 17 18 19 20 21 22 23 24 25 26 27 30 31 32 33 34 35 36 38 40 41 42 44 45 48 50 52 54 55 57 58 59 60 61 63 64 65 67...
result:
ok ok (1000 test cases)
Test #13:
score: 0
Accepted
time: 26ms
memory: 15916kb
input:
1000 137 178 124 131 53 109 99 21 107 122 79 28 80 88 126 9 16 1 29 55 126 54 13 39 135 16 63 56 123 121 27 74 81 95 34 38 49 85 127 135 87 106 91 68 57 124 122 113 87 1 52 104 135 93 132 12 98 83 85 26 66 76 41 82 108 90 88 59 29 15 75 58 36 14 116 65 83 64 21 105 132 13 7 70 97 127 92 112 126 55 1...
output:
4 8 10 18 19 25 27 29 31 36 43 48 49 50 51 52 55 57 58 59 62 63 71 77 79 80 82 83 85 87 88 89 90 91 92 93 95 96 97 100 101 102 103 105 106 107 110 111 112 113 115 116 117 118 119 120 123 125 127 128 129 131 132 133 134 135 136 137 1 3 4 5 6 8 9 10 11 12 15 16 22 24 26 29 30 31 32 34 37 39 40 42 48 ...
result:
ok ok (1000 test cases)
Test #14:
score: -100
Wrong Answer
time: 20ms
memory: 18020kb
input:
1000 148 221 51 9 45 80 86 44 133 98 100 25 130 4 99 17 28 44 131 87 103 87 102 53 115 49 9 5 105 130 11 69 56 23 148 106 106 85 57 102 15 147 100 52 22 10 138 60 38 12 126 119 12 125 86 62 108 123 15 63 90 93 35 116 1 75 63 126 23 127 143 127 114 24 12 133 144 82 12 29 6 51 67 26 129 79 115 16 53 6...
output:
2 3 5 6 7 9 10 11 12 13 14 15 16 17 20 22 26 27 30 31 32 33 35 36 37 38 41 43 44 45 48 49 51 52 53 54 56 58 60 62 63 65 71 72 76 79 80 81 83 84 85 87 89 93 94 97 103 105 106 107 108 109 111 121 122 126 129 130 132 134 137 142 144 145 1 5 6 7 11 20 21 27 29 30 31 32 33 38 39 40 42 44 45 46 47 48 49 ...
result:
wrong output format Expected integer, but "Assertion" found (test case 545)