Alice and Bob are going on a trip. Alice is a lazy girl who wants to minimize the total travelling distance, while Bob as an active boy wants to maximize it. At the same time, they cannot let the value to be less than a given integer L since that will make them miss too much pleasure, and they cannot let the value to be greater than a given integer R since they don't want to get too exhausted.
The city they are visiting has n spots and the spots are connected by directed edges. The spots are connected in such a way that they form a tree and the root will always be at spot 0. They take turns to select which edge to go. Both of them choose optimally. Bob will go first.
The city they are visiting has n spots and the spots are connected by directed edges. The spots are connected in such a way that they form a tree and the root will always be at spot 0. They take turns to select which edge to go. Both of them choose optimally. Bob will go first.
There is a blank line after each test case.
Proceed to the end of file.
3 2 4 0 1 1 0 2 5 7 2 8 0 1 1 0 2 1 1 3 1 1 4 10 2 5 1 2 6 5 7 4 8 0 1 1 0 2 1 1 3 1 1 4 2 2 5 1 2 6 5 4 2 6 0 1 1 1 2 1 1 3 5
Oh, my god! 2 6 2
题意:有n个景点,形成了一棵树,Bob和Alice要从树根处走到一个叶子节点处,Bob和Alice轮流决定下一步走那一条路,Bob要使长度最长,Alice要使长度最短,但是总长度
不能超过R,不能小于L。这题一边dfs过的,加上输入挂,就过了
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=500005;
const int INF=0x3f3f3f3f;
int ans[maxn];
int n,l,r;
struct node
{
int u,w;
node(int u=0,int w=0):u(u),w(w) {}
};
vector<node> v[maxn];
namespace fastIO
{
#define BUF_SIZE 100000
bool IOerror=0;
inline char nc()
{
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend)
{
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1)
{
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch)
{
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
inline void read(int &x)
{
char ch;
while(blank(ch=nc()));
if(IOerror)
return ;
for(x=ch-'0'; (ch=nc())>='0'&&ch<='9'; x=x*10+ch-'0');
}
#undef BUF_SIZE
};
void dfs(int fa,int i,int p)
{
if(ans[i]>r)
{
ans[i]=-1;
return ;
}
if(fa!=-1&&v[i].size()==1)
{
if(ans[i]<l)
ans[i]=-1;
else
ans[i]=0;
return ;
}
int x=ans[i];
int x1=-1,x2=INF;
for(int j=0; j<v[i].size(); j++)
{
int u=v[i][j].u;
if(u==fa)
continue;
int w=v[i][j].w;
ans[u]=x+w;
dfs(i,u,p+1);
if(ans[u]>=0)
{
if(p%2==1)
x1=max(x1,ans[u]+w);
else
x2=min(x2,ans[u]+w);
}
}
if(p%2==1)
ans[i]=x1;
else
ans[i]=x2;
if(ans[i]==INF)
ans[i]=-1;
}
int main()
{
while(fastIO::read(n),!fastIO::IOerror)
{
fastIO::read(l);
fastIO::read(r);
for(int i=0; i<n; i++)
v[i].clear();
for(int i=0; i<n-1; i++)
{
int x,y,c;
fastIO::read(x);
fastIO::read(y);
fastIO::read(c);
v[x].push_back(node(y,c));
v[y].push_back(node(x,c));
}
ans[0]=0;
dfs(-1,0,1);
if(ans[0]==-1)
printf("Oh, my god!\n");
else
printf("%d\n",ans[0]);
}
return 0;
}