QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#177282#5313. Please Save PigelandExplodingKonjacRE 4ms20000kbC++176.9kb2023-09-12 19:47:412023-09-12 19:47:42

Judging History

你现在查看的是最新测评结果

  • [2023-09-12 19:47:42]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:20000kb
  • [2023-09-12 19:47:41]
  • 提交

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 std::string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	void writesp(const T &...x)
	{ std::initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	void writeln(const T &...x)
	{ std::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>
	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->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;
bool vis[500005];
vector<pair<int,int>> G[500005];

class DataSet
{
 private:
	vector<LL> vec;
	LL val;
 public:
	DataSet(): vec{},val{} {}
	DataSet(const DataSet &other): vec(other.vec),val(other.val) {}
	DataSet(DataSet &&other): vec(move(other.vec)),val(other.val) {}
	size_t size()const { return vec.size(); }
	bool empty()const { return vec.empty(); }
	void clear() { vec.clear(),val=0; }
	LL query(LL dt=0)const
	{
		if(vec.empty()) return 0;
		return gcd(vec[0]+dt,val);
	}
	void swap(DataSet &other)
	{
		std::swap(vec,other.vec);
		std::swap(val,other.val);
	}
	void insert(LL x)
	{
		if(vec.empty()) return vec.push_back(x);
		vec.push_back(x-vec[0]);
		val=gcd(val,x-vec[0]);
	}
	void add(LL x)
	{
		if(vec.empty()) return;
		vec[0]+=x;
	}
};

LL f[500005],g[500005];
int siz[500005],son[500005],sonw[500005];

class FSolver
{
 private:
	void dfs1(int u,int fa=0)
	{
		siz[u]=vis[u];
		for(auto &[v,w]: G[u])
		{
			if(v==fa) continue;
			dfs1(v,u);
			siz[u]+=siz[v];
			f[u]+=f[v]+w*siz[v];
		}
	}
	void dfs2(int u,int fa=0)
	{
		LL sz=vis[u],sm=0;
		for(auto &[v,w]: G[u])
		{
			sz+=siz[v];
			sm+=f[v]+w*siz[v];
		}
		for(auto &[v,w]: G[u])
		{
			if(v==fa) continue;
			siz[u]=sz-siz[v];
			f[u]=sm-f[v]-w*siz[v];
			dfs2(v,u);
		}
		siz[u]=sz,f[u]=sm;
	}
 public:
	void main() { dfs1(1),dfs2(1); }
}fsolver;

class GSolver
{
 private:
	void dfs1(int u,int fa=0)
	{
		siz[u]=1;
		for(auto &[v,w]: G[u])
		{
			if(v==fa) continue;
			dfs1(v,u),siz[u]+=siz[v];
			if(siz[v]>siz[son[u]]) son[u]=v,sonw[u]=w;
		}
	}
	void dfs2(int u,DataSet &ds,int op,LL d=0,int fa=0)
	{
		for(auto &[v,w]: G[u])
			if(v!=fa) dfs2(v,ds,op,d+w,u);
		if(op==1) g[u]=gcd(g[u],ds.query(d));
		else if(vis[u]) ds.insert(d);
	}
	void solve1(int u,DataSet &cur,int fa=0)
	{
		for(auto &[v,w]: G[u])
		{
			if(v==fa || v==son[u]) continue;
			DataSet nxt;
			solve1(v,nxt,u);
			dfs2(v,cur,1,w,u);
			dfs2(v,cur,2,w,u);
		}
		g[u]=gcd(g[u],cur.query());
		if(son[u])
		{
			if(vis[u]) cur.insert(0);
			cur.add(sonw[u]);
			solve1(son[u],cur,u);
		}
	}
	DataSet solve2(int u,int fa=0)
	{
		DataSet cur;
		if(son[u])
		{
			solve2(son[u],u).swap(cur);
			cur.add(sonw[u]);
		}
		for(auto it=G[u].rbegin();it!=G[u].rend();it++)
		{
			auto &[v,w]=*it;
			if(v==fa || v==son[u]) continue;
			solve2(v,u);
			dfs2(v,cur,1,w,u);
			dfs2(v,cur,2,w,u);
		}
		g[u]=gcd(g[u],cur.query());
		if(vis[u]) cur.insert(0);
		return cur;
	}
 public:
	void main()
	{
		dfs1(1);
		DataSet initial;
		solve1(1,initial);
		solve2(1);
	}
}gsolver;

int main()
{
	qin>>n>>m;
	for(int i=1,x;i<=m;i++) qin>>x,vis[x]=true;
	for(int i=1,u,v,w;i<n;i++)
	{
		qin>>u>>v>>w;
		G[u].emplace_back(v,w);
		G[v].emplace_back(u,w);
	}
	fsolver.main();
	gsolver.main();
	LL ans=4e18;
	for(int i=1;i<=n;i++)
		ans=min(ans,f[i]?f[i]/g[i]:0);
	qout<<ans*2<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 18012kb

input:

5 3
3 4 5
1 2 2
2 3 4
2 5 4
3 4 6

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 4ms
memory: 18048kb

input:

10 3
1 7 10
7 6 3
1 8 3
3 6 3
8 6 2
4 1 1
10 6 4
2 8 3
9 10 3
5 10 3

output:

24

result:

ok 1 number(s): "24"

Test #3:

score: 0
Accepted
time: 0ms
memory: 20000kb

input:

1 1
1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: -100
Runtime Error

input:

100000 1
79187
72704 72659 15
32741 43496 10
21580 97447 17
55758 36700 21
32116 3643 14
60460 58764 12
75894 50624 7
58295 49393 22
43733 17210 1
58093 68769 15
1086 58916 17
25632 37710 11
49555 92976 8
32547 27060 18
84896 12811 1
3196 1242 16
18870 78236 14
2414 7945 12
48745 15399 1
17648 83791...

output:


result: