QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123114 | #6413. Classical Graph Theory Problem | ExplodingKonjac | WA | 4ms | 17848kb | C++17 | 6.7kb | 2023-07-11 19:09:01 | 2023-07-11 19:09:03 |
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 tmp=0;
for(auto &[v,w]: g2[u]) merge(u,v),tmp+=ans[v];
sum[u]+=(ans[u]=(tmp<=0?1:-1));
for(auto &[v,w]: g2[u])
if(ans[u]==ans[v])
merge(u,v),merge(u,w),sum[u]+=(ans[w]=-ans[u]);
for(auto &[v,w]: g2[u])
if(ans[u]!=ans[v])
merge(u,v),merge(u,w),sum[u]+=(ans[w]=(sum[u]>0?-1:1));
for(auto &[v,w]: g1[u])
if(!del[w] && deg[v]==1)
merge(u,v),sum[u]+=(ans[v]=-ans[u]);
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: 0ms
memory: 15704kb
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:
3 4 5 2
result:
ok ok (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 17848kb
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:
Assertion abs(sum[u])<=1 failed. 1 1 2 3 4 5 8 9 11 12 13 19 20 21 29 1 5 6 10 11 13 1 2 3 4 9 10
result:
wrong output format Expected integer, but "Assertion" found (test case 1)