QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#328110 | #837. Giant Penguin | ExplodingKonjac | WA | 47ms | 5964kb | C++20 | 7.0kb | 2024-02-15 17:19:27 | 2024-02-15 17:19:27 |
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
{
public:
FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
~FastOutput() { flush(); }
void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
void put(char x)
{
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
putc(x,target);
#endif
}
FastOutput &operator <<(char x)
{ return put(x),*this; }
FastOutput &operator <<(const char *s)
{ for(;*s;put(*(s++)));return *this; }
FastOutput &operator <<(const std::string &s)
{ return (*this)<<s.c_str(); }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
{
if(x<0) return put('-'),(*this)<<(-x);
char stk[std::numeric_limits<T>::digits10+1],*top=stk;
do *(top++)=x%10+'0',x/=10; while(x);
while(top!=stk) put(*(--top));
return *this;
}
template<typename ...T>
void writesp(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
template<typename ...T>
void writeln(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
template<typename Iter>
std::enable_if_t<std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>
::value> writesp(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<' '; }
template<typename Iter>
std::enable_if_t<std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>
::value> writeln(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;
class FastInput final: public FastIOBase
{
private:
bool __eof;
public:
FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{ buf_p=BUFSIZE; }
void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
bool eof()const { return buf[buf_p]==EOF; }
#else
{}
bool eof()const { return feof(target); }
#endif
char get()
{
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 unget(char c)
{
__eof=false;
#ifdef OPENIOBUF
buf[--buf_p]=c;
#else
ungetc(c,target);
#endif
}
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=get()));return *this; }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
{
char ch,sym=0;x=0;
while(isspace(ch=get()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=get();
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
return unget(ch),sym?x=-x:x,*this;
}
FastInput &operator >>(char *s)
{
while(isspace(*s=get()));
if(__eof) return *this;
for(;!isspace(*s) && !__eof;*(++s)=get());
return unget(*s),*s='\0',*this;
}
FastInput &operator >>(std::string &s)
{
char str_buf[(1<<8)+1]={0},*p=str_buf;
char *const buf_end=str_buf+(1<<8);
while(isspace(*p=get()));
if(__eof) return *this;
for(s.clear(),p++;;p=str_buf)
{
for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
if(p!=buf_end) break;
s.append(str_buf);
}
unget(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &&...x)
{ std::initializer_list<int>{((*this)>>(x),0)...}; }
template<typename Iter>
std::enable_if_t<std::is_base_of<
std::forward_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category>
::value> 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;
constexpr int INF=1e9;
#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define LOG(...) [](auto...){}(__VA_ARGS__)
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif
constexpr int MAXN=1e5;
int n,m,q,K;
vector<int> G1[MAXN+5],G2[MAXN+5];
int fa[MAXN+5];
inline int findFa(int x)
{ return x!=fa[x]?fa[x]=findFa(fa[x]):x; }
inline void merge(int x,int y)
{ fa[findFa(y)]=findFa(x); }
int wgt[MAXN+5],anc[MAXN+5],bel[MAXN+5],ord[MAXN+5];
int tot,id1[MAXN+5][20],id2[MAXN+5][20],mn[MAXN+5];
bool vis[MAXN+5];
vector<int> dis[10*MAXN+5],spc[MAXN+5];
void getWeight(int u,int fa=0)
{
int x=0,y=0;
for(auto &v: G2[u])
{
if(v==fa) continue;
getWeight(v,u);
x|=(wgt[v]&y),y|=wgt[v];
}
int k=(x?__lg(x)+1:0);
wgt[u]=((y>>k)+1)<<k;
}
void dfs1(int u,int tp,vector<int> &V,int fa=0)
{
V.push_back(u);
bel[u]=tp;
for(auto &v: G2[u])
if(v!=fa && !vis[v])
dfs1(v,fa?tp:v,V,u);
}
void dfs2(int u,vector<int> &res,int fa=0)
{
bool fl=true;
for(auto &v: G1[u])
if(u<v && bel[v] && bel[u]!=bel[v])
fl=true;
if(fl) res.push_back(u);
for(auto &v: G2[u])
if(v!=fa && !vis[v])
dfs2(v,res,u);
}
void bfs(int S,int d,int sz)
{
auto &res=dis[id2[S][d]];
res.assign(sz,INF);
queue<int> q;
q.push(S),res[id1[S][d]]=0;
while(!q.empty())
{
int u=q.front(),du=res[id1[u][d]];
q.pop();
for(auto &v: G1[u])
{
int &dv=res[id1[v][d]];
if(du+1>=dv) continue;
dv=du+1,q.push(v);
}
}
}
int main()
{
qin>>n>>m>>K;
iota(fa+1,fa+n+1,1);
for(int i=1;i<=m;i++)
{
int u,v;
qin>>u>>v;
G1[u].push_back(v);
G1[v].push_back(u);
if(findFa(u)!=findFa(v))
{
G2[u].push_back(v);
G2[v].push_back(u);
merge(u,v);
}
}
getWeight(1);
for(int i=1;i<=n;i++) wgt[i]=__builtin_ctz(wgt[i]);
iota(ord+1,ord+n+1,1);
sort(ord+1,ord+n+1,[](int x,int y){
return wgt[x]>wgt[y];
});
for(int i=1;i<=n;i++)
{
int s=ord[i],d=wgt[s],mn=1e9;
vector<int> V;
auto search=[&](auto &self,int u,int fa)->void {
for(auto &v: G2[u])
{
if(v==fa) continue;
if(wgt[v]<wgt[s]) self(self,v,u);
else if(wgt[v]<mn) mn=wgt[v],anc[s]=v;
}
};
search(search,s,s);
dfs1(s,s,V),dfs2(s,spc[s]);
for(int j=0;j<(int)V.size();j++)
id1[V[j]][d]=j;
for(auto &j: spc[s])
id2[j][d]=++tot,bfs(j,d,V.size());
vis[s]=true;
}
fill(mn+1,mn+tot+1,INF);
qin>>q;
while(q--)
{
int opt,u;
qin>>opt>>u;
if(opt==1)
{
for(int i=u;i;i=anc[i])
{
int k=wgt[i];
for(auto &v: spc[i])
{
int x=id2[v][k];
mn[x]=min(mn[x],dis[x][id1[u][k]]);
}
}
}
else
{
int ans=INF;
for(int i=u;i;i=anc[i])
{
int k=wgt[i];
for(auto &v: spc[i])
{
int x=id2[v][k];
ans=min(ans,dis[x][id1[u][k]]+mn[x]);
}
}
qout<<ans<<'\n';
}
}
return 0;
}
/*
5 4 0
1 2
2 3
3 4
4 5
7
1 1
1 5
2 1
2 2
2 3
2 4
2 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5920kb
input:
5 4 0 1 2 2 3 3 4 4 5 7 1 1 1 5 2 1 2 2 2 3 2 4 2 5
output:
0 1 2 1 0
result:
ok 5 number(s): "0 1 2 1 0"
Test #2:
score: 0
Accepted
time: 3ms
memory: 5640kb
input:
5 6 2 1 2 2 3 1 3 3 4 4 5 3 5 3 1 1 2 4 2 5
output:
2 2
result:
ok 2 number(s): "2 2"
Test #3:
score: -100
Wrong Answer
time: 47ms
memory: 5964kb
input:
100 99 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...
output:
99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 3 2 3 4 5 6 7 8 3 2 3 4 3 2 1 0 99 98 97 96 95 94 93 92 ...
result:
wrong answer 70th numbers differ - expected: '30', found: '2'