传送门

分析

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

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

于是问题就转换成了$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. mac快捷键整理以及node的基本使用

    该文章是作为日常积累和整理,又是好久没有整理node的相关知识了,最近翻了翻自己的有道云笔记,怎一个乱字了的,重新整理下. 一.Mac常用快捷键 Command+M: 最小化窗口 Command+T: ...

  2. uva1315 Crazy tea party(找规律)

    题意就是说把顺时针排的1到n换成逆时针排的需要的最少交换步数. 如果是线形的一串数,需要的交换次数就是个冒泡排序的交换次数:n*(n-1)/2,或者用a[i]=(i-1)+a[i-1]推出来. 对于环 ...

  3. 统计日志中ip出现的次数

    grep -r 'GET /weixin/weixin_izp/index.html' ./chunyun.access.log > ~/access.log cat access.log |a ...

  4. POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)

    A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...

  5. python函数之sorted与sort

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...

  6. Vue forms

    Vue forms Vue 的表单. 表单中的数据和是双向绑定的. 你可以使用 v-model 对控件元素进行数据双向绑定. 比较有用的修饰符 .lazy .number .trim

  7. bzoj 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头——枚举倍数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1607 #include<iostream> #include<cstdio ...

  8. BZOJ1999 树网的核[数据加强版]

    1999: [Noip2007]Core树网的核 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1214  Solved: 336[Submit][St ...

  9. 机器学习:SVM(基础理解)

    一.基础理解 1)简介 SVM(Support Vector Machine):支撑向量机,既可以解决分类问题,又可以解决回归问题: SVM 算法可分为:Hard Margin SVM.Soft Ma ...

  10. JavaScript,Dom,jQuery

    JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript 语言的规则编写相应代码,浏览器可以解释出相应的处理. 注 ...