Phoenix and Odometers solution codeforces – In Fire City, there are nn intersections and mm one-way roads, 2021

  Phoenix and Odometers solution codeforces

Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces

In Fire City, there are nn intersections and mm one-way roads. The ii-th road goes from intersection aiai to bibi and has length lili miles.

There are qq cars that may only drive along those roads. The ii-th car starts at intersection vivi and has an odometer that begins at sisi, increments for each mile driven, and resets to 00 whenever it reaches titi. Phoenix has been tasked to drive cars along some roads (possibly none) and return them to their initial intersection with the odometer showing 00.

For each car, please find if this is possible.

A car may visit the same road or intersection an arbitrary number of times. The odometers don’t stop counting the distance after resetting, so odometers may also be reset an arbitrary number of times.

Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces

Input  Phoenix and Odometers solution codeforces

The first line of the input contains two integers nn and mm (2n21052≤n≤2⋅1051m21051≤m≤2⋅105) — the number of intersections and the number of roads, respectively.

Each of the next mm lines contain three integers aiaibibi, and lili (1ai,bin1≤ai,bi≤naibiai≠bi1li1091≤li≤109) — the information about the ii-th road. The graph is not necessarily connected. It is guaranteed that between any two intersections, there is at most one road for each direction.

The next line contains an integer qq (1q21051≤q≤2⋅105) — the number of cars.

Each of the next qq lines contains three integers vivisisi, and titi (1vin1≤vi≤n0si<ti1090≤si<ti≤109) — the initial intersection of the ii-th car, the initial number on the ii-th odometer, and the number at which the ii-th odometer resets, respectively.

Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces

Output Phoenix and Odometers solution codeforces

Print qq answers. If the ii-th car’s odometer may be reset to 00 by driving through some roads (possibly none) and returning to its starting intersection vivi, print YES. Otherwise, print NO.

Examples Phoenix and Odometers solution codeforces

input

Copy Phoenix and Odometers solution codeforces
4 4
1 2 1
2 3 1
3 1 2
1 4 3
3
1 1 3
1 2 4
4 0 1

output

Copy
YES
NO
YES

input

Copy
4 5
1 2 1
2 3 1
3 1 2
1 4 1
4 3 2
2
1 2 4
4 3 5

output

Copy
YES
YES
Note

The illustration for the first example is below:

In the first query, Phoenix can drive through the following cities: 11  22  33  11  22  33  11. The odometer will have reset 33 times, but it displays 00 at the end.

In the second query, we can show that there is no way to reset the odometer to 00 and return to intersection 11.

In the third query, the odometer already displays 00, so there is no need to drive through any roads.

Below is the illustration for the second example:

Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces

C++

Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces

#include<bits/stdc++.h>
using namespace std;
const int N=200005;
int n,m,q,t,k,dfn[N],low[N],st[N],tp,in[N],b[N];
long long d[N],gd[N],gs[N];
vector<pair<int,int>>e[N];
void dfs(int u)
{
dfn[u]=low[u]=++t;
st[++tp]=u,in[u]=1;
for(auto i:e[u])
{
int v=i.first,w=i.second;
if(!dfn[v])
{
d[v]=d[u]+w;
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(in[v])
{
low[u]=min(low[u],dfn[v]);
gd[u]=__gcd(gd[u],d[u]-d[v]+w);
}
}
if(low[u]==dfn[u])
{
k++;
int v;
do
{
v=st[tp--];
in[v]=0;
b[v]=k;
gs[k]=__gcd(gs[k],gd[v]);
}while(v!=u);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[u].emplace_back(v,w);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
dfs(i);
scanf("%d",&q);
while(q--)
{
int u,s,t;
scanf("%d%d%d",&u,&s,&t);
if(!s)
puts("YES");
else if(!gs[b[u]])
puts("NO");
else
puts(s%__gcd(gs[b[u]],(long long)t)?"NO":"YES");
}
return 0;
}

JAVA

Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces

import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;

public class G {
InputStream is;
FastWriter out;
String INPUT = "";

void solve() {
int n = ni(), m = ni();
int[] from = new int[m];
int[] to = new int[m];
int[] ws = new int[m];
for (int i = 0; i < m; i++) {
from[i] = ni() - 1;
to[i] = ni() - 1;
ws[i] = ni();
}
int[][][] g = packWD(n, from, to, ws);
int[][][] ig = packWD(n, to, from, ws);
boolean[] ved = new boolean[n];
int[] clus = decomposeToSCC(g);

long[] gws = new long[n];
Arrays.fill(gws, Long.MAX_VALUE);
long[] pluss = new long[n];
for(int i = 0;i < n;i++){
if(!ved[i]){
gws[i] = 0;
dfs(i, 0L, 0L, g, gws, pluss, clus, ved);
}
}
int u = clus.length;
long[] ps = new long[u];
for(int i = 0;i < n;i++){
ps[clus[i]] = gcd(ps[clus[i]], pluss[i]);
}
for(int Q = ni();Q > 0;Q--){
int v = ni()-1, s = ni(), t = ni();
if(s == 0){
out.println("YES");
}else{
long P = ps[clus[v]];
if(P == 0){
out.println("NO");
}else if((t-s) % gcd(P, t) == 0){
out.println("YES");
}else{
out.println("NO");
}
}
}
}

public static int[] decomposeToSCC(int[][][] g)
{
int n = g.length;
Context cx = new Context(g);
for(int i = 0;i < n;i++){
if(cx.ord[i] == -1)dfs(i, cx);
}
return cx.clus;
}

public static class Context
{
int[][][] g;
int[] ord, low;
int id;
int[] hist;
boolean[] inhist;
int hp;
int[] clus;
int cid;

public Context(int[][][] g){
int n = g.length;
this.g = g;
ord = new int[n];
Arrays.fill(ord, -1);
low = new int[n];
Arrays.fill(low, -1);
id = 0;
hp = 0;
cid = 0;
hist = new int[n];
clus = new int[n];
Arrays.fill(clus, -1);
inhist = new boolean[n];
}
}

private static void dfs(int cur, Context cx)
{
cx.ord[cur] = cx.low[cur] = cx.id++;
cx.hist[cx.hp++] = cur;
cx.inhist[cur] = true;
for(int[] nex : cx.g[cur]){
if(cx.ord[nex[0]] == -1){
// unvisited
dfs(nex[0], cx);
cx.low[cur] = Math.min(cx.low[cur], cx.low[nex[0]]);
}else if(cx.inhist[nex[0]]){
cx.low[cur] = Math.min(cx.low[cur], cx.ord[nex[0]]);
}
}

if(cx.ord[cur] == cx.low[cur]){
while(true){
int tar = cx.hist[--cx.hp];
cx.inhist[tar] = false;
cx.clus[tar] = cx.cid;
if(tar == cur)break;
}
cx.cid++;
}
}

public static long gcd(long a, long b) {
while (b > 0) {
long c = a;
a = b;
b = c % b;
}
return a;
}

void dfs(int cur, long val, long valp, int[][][] g, long[] ws, long[] pluss, int[] clus, boolean[] ved)
{
boolean oved = ved[cur];
ved[cur] = true;
long oplus = pluss[cur];
if(ws[cur] == Long.MAX_VALUE){
ws[cur] = val;
}else{
pluss[cur] = gcd(pluss[cur], Math.abs(ws[cur] - val));
}
if(ws[cur] == Long.MAX_VALUE){
ws[cur] = valp;
}else{
pluss[cur] = gcd(pluss[cur], Math.abs(ws[cur] - valp));
}

if(oplus != pluss[cur] || !oved){
for (int[] e : g[cur]) {
if (clus[cur] != clus[e[0]]) continue;
dfs(e[0], ws[cur] + e[1], ws[cur] + e[1] + pluss[cur], g, ws, pluss, clus, ved);
}
}
}

public static int[][][] packWD(int n, int[] from, int[] to, int[] w) {
return packWD(n, from, to, w, from.length);
}

public static int[][][] packWD(int n, int[] from, int[] to, int[] w, int sup) {
int[][][] g = new int[n][][];
int[] p = new int[n];
for (int i = 0; i < sup; i++) p[from[i]]++;
for (int i = 0; i < n; i++) g[i] = new int[p[i]][2];
for (int i = 0; i < sup; i++) {
--p[from[i]];
g[from[i]][p[from[i]]][0] = to[i];
g[from[i]][p[from[i]]][1] = w[i];
}
return g;
}


void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new FastWriter(System.out);

// long s = System.currentTimeMillis();
// solve();
// out.flush();
// tr(System.currentTimeMillis()-s+"ms");
Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
@Override
public void run() {
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
};
t.start();
t.join();
}

public static void main(String[] args) throws Exception { new G().run(); }

private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;

private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}

private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }

private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}

private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}

private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}

private long[] nal(int n)
{
long[] a = new long[n];
for(int i = 0;i < n;i++)a[i] = nl();
return a;
}

private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}

private int[][] nmi(int n, int m) {
int[][] map = new int[n][];
for(int i = 0;i < n;i++)map[i] = na(m);
return map;
}

private int ni() { return (int)nl(); }

private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}

while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}

public static class FastWriter
{
private static final int BUF_SIZE = 1<<13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;

private FastWriter(){out = null;}

public FastWriter(OutputStream os)
{
this.out = os;
}

public FastWriter(String path)
{
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}

public FastWriter write(byte b)
{
buf[ptr++] = b;
if(ptr == BUF_SIZE)innerflush();
return this;
}

public FastWriter write(char c)
{
return write((byte)c);
}

public FastWriter write(char[] s)
{
for(char c : s){
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
}
return this;
}

public FastWriter write(String s)
{
s.chars().forEach(c -> {
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
});
return this;
}

private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}

public FastWriter write(int x)
{
if(x == Integer.MIN_VALUE){
return write((long)x);
}
if(ptr + 12 >= BUF_SIZE)innerflush();
if(x < 0){
write((byte)'-');
x = -x;
}
int d = countDigits(x);
for(int i = ptr + d - 1;i >= ptr;i--){
buf[i] = (byte)('0'+x%10);
x /= 10;
}
ptr += d;
return this;
}

private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}

public FastWriter write(long x)
{
if(x == Long.MIN_VALUE){
return write("" + x);
}
if(ptr + 21 >= BUF_SIZE)innerflush();
if(x < 0){
write((byte)'-');
x = -x;
}
int d = countDigits(x);
for(int i = ptr + d - 1;i >= ptr;i--){
buf[i] = (byte)('0'+x%10);
x /= 10;
}
ptr += d;
return this;
}

public FastWriter write(double x, int precision)
{
if(x < 0){
write('-');
x = -x;
}
x += Math.pow(10, -precision)/2;
// if(x < 0){ x = 0; }
write((long)x).write(".");
x -= (long)x;
for(int i = 0;i < precision;i++){
x *= 10;
write((char)('0'+(int)x));
x -= (int)x;
}
return this;
}

public FastWriter writeln(char c){
return write(c).writeln();
}

public FastWriter writeln(int x){
return write(x).writeln();
}

public FastWriter writeln(long x){
return write(x).writeln();
}

public FastWriter writeln(double x, int precision){
return write(x, precision).writeln();
}

public FastWriter write(int... xs)
{
boolean first = true;
for(int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}

public FastWriter write(long... xs)
{
boolean first = true;
for(long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}

public FastWriter writeln()
{
return write((byte)'\n');
}

public FastWriter writeln(int... xs)
{
return write(xs).writeln();
}

public FastWriter writeln(long... xs)
{
return write(xs).writeln();
}

public FastWriter writeln(char[] line)
{
return write(line).writeln();
}

public FastWriter writeln(char[]... map)
{
for(char[] line : map)write(line).writeln();
return this;
}

public FastWriter writeln(String s)
{
return write(s).writeln();
}

private void innerflush()
{
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}

public void flush()
{
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}

public FastWriter print(byte b) { return write(b); }
public FastWriter print(char c) { return write(c); }
public FastWriter print(char[] s) { return write(s); }
public FastWriter print(String s) { return write(s); }
public FastWriter print(int x) { return write(x); }
public FastWriter print(long x) { return write(x); }
public FastWriter print(double x, int precision) { return write(x, precision); }
public FastWriter println(char c){ return writeln(c); }
public FastWriter println(int x){ return writeln(x); }
public FastWriter println(long x){ return writeln(x); }
public FastWriter println(double x, int precision){ return writeln(x, precision); }
public FastWriter print(int... xs) { return write(xs); }
public FastWriter print(long... xs) { return write(xs); }
public FastWriter println(int... xs) { return writeln(xs); }
public FastWriter println(long... xs) { return writeln(xs); }
public FastWriter println(char[] line) { return writeln(line); }
public FastWriter println(char[]... map) { return writeln(map); }
public FastWriter println(String s) { return writeln(s); }
public FastWriter println() { return writeln(); }
}

public void trnz(int... o)
{
for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" ");
System.out.println();
}

// print ids which are 1
public void trt(long... o)
{
Queue<Integer> stands = new ArrayDeque<>();
for(int i = 0;i < o.length;i++){
for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x));
}
System.out.println(stands);
}

public void tf(boolean... r)
{
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}

public void tf(boolean[]... b)
{
for(boolean[] r : b) {
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
System.out.println();
}

public void tf(long[]... b)
{
if(INPUT.length() != 0) {
for (long[] r : b) {
for (long x : r) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
System.out.println();
}
}

public void tf(long... b)
{
if(INPUT.length() != 0) {
for (long x : b) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
}

private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


Rust

// ---------- begin SCC ----------
pub struct SCC {
size: usize,
edge: Vec<(u32, u32)>,
}

impl SCC {
pub fn new(size: usize) -> Self {
assert!(size <= 10usize.pow(8));
SCC { size, edge: vec![] }
}
pub fn add_edge(&mut self, a: usize, b: usize) {
assert!(a < self.size && b < self.size);
self.edge.push((a as u32, b as u32));
}
pub fn build(&self) -> (usize, Vec<usize>) {
let size = self.size;
let mut start = vec![0u32; size + 1];
self.edge.iter().for_each(|e| start[e.0 as usize + 1] += 1);
for i in 1..=size {
start[i] += start[i - 1];
}
let mut buf = vec![0; self.edge.len()];
for &(a, b) in self.edge.iter() {
let po = &mut start[a as usize];
buf[*po as usize] = b;
*po += 1;
}
let mut s = 0usize;
let mut neighbor = start
.into_iter()
.take(size)
.map(|t| {
let t = t as usize;
let it = buf[s..t].iter().map(|p| *p as usize);
s = t;
it
})
.collect::<Vec<_>>();
let mut ord = vec![size; size];
let mut assigned = vec![false; size];
let mut stack_s = vec![];
let mut stack_p = vec![];
let mut call = vec![];
let mut now_ord = 0;
let mut res = vec![0; size];
let mut id = 0;
enum Operation {
Call(usize),
Iter(usize),
Eval(usize),
}
for i in 0..size {
if ord[i] != size {
continue;
}
call.push(Operation::Call(i));
while let Some(op) = call.pop() {
match op {
Operation::Call(v) => {
ord[v] = now_ord;
now_ord += 1;
stack_s.push(v);
stack_p.push(v);
call.push(Operation::Eval(v));
call.push(Operation::Iter(v));
}
Operation::Iter(v) => {
let it = &mut neighbor[v];
while let Some(u) = it.next() {
if ord[u] == size {
call.push(Operation::Iter(v));
call.push(Operation::Call(u));
break;
} else if !assigned[u] {
while ord[*stack_p.last().unwrap()] > ord[u] {
stack_p.pop();
}
}
}
}
Operation::Eval(v) => {
if *stack_p.last().unwrap() == v {
while let Some(u) = stack_s.pop() {
res[u] = id;
assigned[u] = true;
if u == v {
break;
}
}
stack_p.pop();
id += 1;
}
}
}
}
}
res.iter_mut().for_each(|v| *v = id - 1 - *v);
(id, res)
}
}
// ---------- end SCC ----------
// ---------- begin binary_gcd ----------
pub fn binary_gcd(a: u64, b: u64) -> u64 {
if a == 0 || b == 0 {
return a + b;
}
let x = a.trailing_zeros();
let y = b.trailing_zeros();
let mut a = a >> x;
let mut b = b >> y;
while a != b {
let x = (a ^ b).trailing_zeros();
if a < b {
std::mem::swap(&mut a, &mut b);
}
a = (a - b) >> x;
}
a << x.min(y)
}
// ---------- end binary_gcd ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}

macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}

macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
// ---------- end input macro ----------

use std::io::Write;

fn dfs(
v: usize,
g: &[Vec<(usize, u64)>],
ord: &mut [usize],
id: &mut usize,
depth: &mut [u64],
ans: &mut Option<u64>,
) {
ord[v] = *id;
*id += 1;
for &(u, w) in g[v].iter() {
if ord[u] >= *id {
depth[u] = depth[v] + w;
dfs(u, g, ord, id, depth, ans);
} else {
let a = depth[v] + w;
let b = depth[u];
let d = a.max(b) - a.min(b);
if d != 0 {
if let Some(g) = ans.as_mut() {
*g = binary_gcd(*g, d);
} else {
*ans = Some(d)
}
}
}
}
}

fn run() {
input! {
n: usize,
m: usize,
e: [(usize1, usize1, u64); m],
q: usize,
ask: [(usize1, u64, u64); q],
}
let mut scc = SCC::new(n);
for &(a, b, _) in e.iter() {
scc.add_edge(a, b);
}
let (len, id) = scc.build();
let mut g = vec![vec![]; n];
for (a, b, l) in e {
if id[a] == id[b] {
g[a].push((b, l));
}
}
let mut root = vec![n; len];
for i in 0..n {
root[id[i]] = i;
}
let mut ans = vec![None; len];
let mut depth = vec![0; n];
let mut ord = vec![n; n];
let mut val = 0;
for i in 0..len {
dfs(root[i], &g, &mut ord, &mut val, &mut depth, &mut ans[i]);
}
let out = std::io::stdout();
let mut out = std::io::BufWriter::new(out.lock());
for (v, s, t) in ask {
let r = id[v];
let mut v = "NO";
if let Some(d) = ans[r] {
let a = binary_gcd(t, d);
if (t - s) % a == 0 {
v = "YES";
}
}
if s == 0 {
v = "YES";
}
writeln!(out, "{}", v).ok();
}
}

fn main() {
run();
}


Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces
Phoenix and Odometers solution codeforces - In Fire City, there are nn intersections and mm one-way roads, 2021 Splitting a String Into Descending Consecutive Values Minimum Adjacent Swaps to Reach the Kth Smallest Number Minimum Interval to Include Each Query
Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces Phoenix and Odometers solution codeforces
 
 

Leave a Comment