传送门

分析

在经过若干次操作之后一定会产生一堆环

而我们又发现从一个点到另一个点实际可以经过所有环

于是问题就转换成了$k_1s_1 + k_2s_2 + ... + len = t$

其中$s_i$为每个环的长度,$len$为两点间距离

于是每次gcd求一下就行了

注意两点间距离不用求LCA,用深度值减一下就可以了

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
long long d[];
vector<pair<int,int> >v[];
inline void dfs(int x,int fa){
long long i,j,k;
for(i=;i<v[x].size();i++){
int y=v[x][i].first,z=v[x][i].second;
if(y==fa)continue;
d[y]=d[x]+z;
dfs(y,x);
}
return;
}
inline long long gcd(long long x,long long y){return y==?x:gcd(y,x%y);}
int main(){
int n,m,i,j,k;
long long res=-;
scanf("%d%d",&n,&m);
for(i=;i<n;i++){
int x,y;
int z;
scanf("%d%d%d",&x,&y,&z);
v[x].push_back(make_pair(y,z));
v[y].push_back(make_pair(x,-z));
}
d[]=;
dfs(,);
for(i=;i<=m;i++){
int x,y;
int z;
scanf("%d",&k);
scanf("%d%d%d",&x,&y,&z);
if(k==){
long long t=d[x]-d[y]+z;
if(res==-){
res=abs(t);
}else {
res=gcd(res,abs(t));
}
}else {
long long t=d[y]-d[x]-z;
if(res!=-){
if(abs(t)%res==)puts("yes");
else puts("no");
}else {
if(t==)puts("yes");
else puts("no");
}
}
}
return ;
}

EZOJ #79的更多相关文章

  1. P87LPC760/61/62/64/67/68/69/78/79芯片解密单片机破解价格

    NXP恩智浦P87LPC760/61/62/64/67/68/69/78/79芯片解密单片机破解 NXP LPC700系列单片机解密型号: P87LPC759.P87LPC760.P87LPC761. ...

  2. LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)

    LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...

  3. Changing SharePoint Default port ( 80 ) to another port ( 79 ).

      Introduction In this How-To I will change my port from 80 to 79, probably because I want to host s ...

  4. Scala 深入浅出实战经典 第79讲:单例深入讲解及单例背后的链式表达式

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  5. 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame

    [源码下载] 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame 作者:webabcd 介绍重新想象 Windows 8.1 St ...

  6. 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹

    一,    最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...

  7. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

  8. 物联网操作系统HelloX V1.79发布公告

    经过HelloX开发团队近半年的努力,在HelloX V1.78版本基础上,增加许多功能特性,并对V1.78版本的一些特性进行了进一步优化之后,正式形成HelloX V1.79测试版本.经相对充分的测 ...

  9. nyoj 79 拦截导弹 (动态规划)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=79 题意即求最长单调递减子序列 #include<iostream> #inc ...

随机推荐

  1. 面试题46:求1+2+...+n

    题目:求1+2+...+n,要求不能使用乘除法.for.while.if.else.swithc.case等关键字及条件判断语句(A?B:C). 解法一:利用构造函数求解 class Temp { p ...

  2. L107

    It is advisable to take an open- minded approach to new idea. 对新思想采取不存先入之见的态度是明智的.That said, the com ...

  3. RequireJs 源码解读及思考

    写在前面: 最近做的一个项目,用的require和backbone,对两者的使用已经很熟悉了,但是一直都有好奇他们怎么实现的,一直寻思着读读源码.现在项目结束,终于有机会好好研究一下. 本文重要解读r ...

  4. Bender Problem

    Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n ...

  5. Equation

    You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of th ...

  6. loj 6083.「美团 CodeM 资格赛」数码

    题目: 给定两个整数\(l\)和\(r\),对于任意\(x\),满足\(l\leq x\leq r\),把\(x\)所有约数写下来. 对于每个写下来的数,只保留最高位的那个数码.求\([1,9]\)中 ...

  7. BZOJ1047:[HAOI2007]理想的正方形

    浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...

  8. 第11篇 PSR-0 规范

    Mandatory A fully-qualified namespace and class must have the following structure \<Vendor Name&g ...

  9. springmvc 加载静态文件失败

    header.jsp,部分代码 <head> <title>QA|VIS_PLATFORM</title> <meta content="width ...

  10. 几种排序方式的java实现(02:希尔排序,归并排序,堆排序)

    代码(部分为别人代码): 1.希尔排序(ShellSort) /* * 希尔排序:先取一个小于n的整数d1作为第一个增量, * 把文件的全部记录分成(n除以d1)个组.所有距离为d1的倍数的记录放在同 ...