QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#125785 | #6738. Cover | ExplodingKonjac | WA | 739ms | 32372kb | C++17 | 6.4kb | 2023-07-17 17:18:06 | 2023-07-17 17:18:09 |
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
{
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(); }
void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#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
{
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_p--;
#else
ungetc(c,target);
#endif
}
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=get()));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=get()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=get();
for(x=0;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 >>(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)
{ 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)
#define LOG(...) void()
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif
int n,m,K;
vector<int> g[100005];
vector<tuple<int,int,int>> ask[100005];
int siz[100005],dep[100005],son[100005],anc[100005],top[100005];
int tot,lb[100005],rb[100005],pos[100005];
void dfs1(int u,int fa=0)
{
siz[u]=1,anc[u]=fa,dep[u]=dep[fa]+1;
int cur=0;
for(auto &v: g[u])
{
if(v==fa) continue;
pos[v]=cur++;
dfs1(v,u),siz[u]=siz[v]+1;
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int tp,int fa=0)
{
lb[u]=++tot,top[u]=tp;
if(son[u]) dfs2(son[u],tp,u);
for(auto &v: g[u])
if(v!=fa && v!=son[u])
dfs2(v,v,u);
rb[u]=tot;
}
class FenwickTree
{
private:
LL t[100005];
public:
void add(int p,LL x) { for(;p<=n;t[p]+=x,p+=p&-p); }
LL get(int p) { LL x=0;for(;p;x+=t[p],p-=p&-p);return x; }
LL get(int l,int r) { return l>r?0:get(r)-get(l-1); }
}t;
LL dp[100005][12],all[100005],tmp[1<<12];
inline int lca(int u,int v)
{
int fu=top[u],fv=top[v];
while(fu!=fv)
if(dep[fu]>dep[fv]) u=anc[fu],fu=top[u];
else v=anc[fv],fv=top[v];
return dep[u]<dep[v]?u:v;
}
inline int jump(int u,int v)
{
int fv=top[v];
while(dep[fv]>dep[u]+1)
v=anc[fv],fv=top[v];
if(dep[fv]==dep[u]+1) return fv;
return son[u];
}
inline LL getsum(int u,int v)
{
int fu=top[u],fv=top[v];
LL res=all[v]-dp[v][pos[son[v]]];
while(fu!=fv)
{
res+=t.get(lb[fv],lb[v]);
int w=anc[fv];
res+=dp[w][pos[fv]]-dp[w][pos[son[w]]];
v=w,fv=top[v];
}
res+=t.get(lb[u],lb[v]);
return res;
}
void dfs3(int u,int fa=0)
{
for(auto &v: g[u]) if(v!=fa) dfs3(v,u);
int sz=g[u].size()-!!fa,M=(1<<sz);
fill(tmp,tmp+M,0);
for(auto &v: g[u])
{
if(v==fa) continue;
int msk=(1<<pos[v]),lim=(M-1)^msk;
for(int S=lim;S;S=(S-1)&lim)
tmp[S|msk]=max(tmp[S|msk],tmp[S]+all[v]);
tmp[msk]=max(tmp[msk],tmp[0]+all[v]);
}
for(auto &[x,y,w]: ask[u])
{
int msk=0,vx=0,vy=0;
LL val=w;
if(x!=u) vx=jump(u,x),val+=getsum(vx,x),msk|=(1<<pos[vx]);
if(y!=u) vy=jump(u,y),val+=getsum(vy,y),msk|=(1<<pos[vy]);
int lim=(M-1)^msk;
for(int S=lim;S;S=(S-1)&lim)
tmp[S|msk]=max(tmp[S|msk],tmp[S]+val);
tmp[msk]=max(tmp[msk],tmp[0]+w);
}
for(int i=0;i<sz;i++) dp[u][i]=tmp[(M-1)^(1<<i)];
all[u]=tmp[M-1];
t.add(lb[u],dp[u][pos[son[u]]]);
}
int main()
{
qin>>n>>m>>K;
for(int i=1,u,v;i<n;i++)
{
qin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(1),dfs2(1,1);
for(int i=1,u,v,w;i<=m;i++)
{
qin>>u>>v>>w;
int l=lca(u,v);
ask[l].emplace_back(u,v,w);
}
dfs3(1);
qout<<all[1]<<'\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 10396kb
input:
5 7 3 1 2 1 3 2 4 2 5 3 2 8 5 4 10 3 1 2 1 2 7 2 1 2 1 2 1 5 2 3
output:
19
result:
ok 1 number(s): "19"
Test #2:
score: -100
Wrong Answer
time: 739ms
memory: 32372kb
input:
100000 500000 12 2 1 3 2 4 2 5 2 6 5 7 2 8 5 9 3 10 2 11 2 12 5 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 12 25 2 26 2 27 2 28 2 29 2 30 15 31 30 32 23 33 26 34 22 35 30 36 26 37 3 38 3 39 3 40 3 41 3 42 3 43 3 44 3 45 3 46 3 47 20 48 21 49 4 50 4 51 4 52 4 53 4 54 4 55 4 56 4 57 4 5...
output:
649940097363
result:
wrong answer 1st numbers differ - expected: '660925834533', found: '649940097363'