message stringlengths 2 45.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 254 108k | cluster float64 3 3 | __index_level_0__ int64 508 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,480 | 3 | 144,960 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
"""{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""
groups = {r: [] for r in self.roots()}
for i in range(self.N):
groups[self.find(i)].append(i)
return groups
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,m,q=map(int,input().split())
uf=UnionFind(n+m)
for _ in range(q):
a,b=map(int,input().split())
uf.union(a-1,b-1+n)
print(uf.group_count()-1)
``` | output | 1 | 72,480 | 3 | 144,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,481 | 3 | 144,962 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
import sys
n,m,q=map(int,input().split())
p=[-1]*(n+m)
r=[0]*(n+m)
def par(i):
if p[i]==-1: return i
p[i]=par(p[i])
return p[i]
def merge(a,b):
a,b=par(a),par(b)
if a==b: return 0
if r[a]<r[b]:p[a]=b
elif r[b]<r[a]:p[b]=a
else:p[a]=b;r[b]+=1
return 1
v=n+m
for l in sys.stdin.read().strip().split('\n') if q else []:
a,b=map(int,l.split())
v-=merge(a-1,b-1+n)
print(v-1)
``` | output | 1 | 72,481 | 3 | 144,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,482 | 3 | 144,964 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
n,m,q = map(int,input().split())
f = [-1]*(n+m+1)
def find(x):
if f[x]==-1:
return x
else:
f[x] = find(f[x])
return f[x]
ans = n+m-1
for i in range(q):
r,c = map(int,input().split())
c+=n
R = find(r)
C = find(c)
if R!=C :
ans-=1
f[R]=C
print(ans)
``` | output | 1 | 72,482 | 3 | 144,965 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,483 | 3 | 144,966 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
class UnionFind:
def __init__(self, n):
self.par = [-1]*n
self.rank = [0]*n
def Find(self, x):
if self.par[x] < 0:
return x
else:
self.par[x] = self.Find(self.par[x])
return self.par[x]
def Unite(self, x, y):
x = self.Find(x)
y = self.Find(y)
if x != y:
if self.rank[x] < self.rank[y]:
self.par[y] += self.par[x]
self.par[x] = y
else:
self.par[x] += self.par[y]
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def Same(self, x, y):
return self.Find(x) == self.Find(y)
def Size(self, x):
return -self.par[self.Find(x)]
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n, m, q = map(int, input().split())
uf = UnionFind(n+m)
for i in range(q):
r, c = map(int, input().split())
r, c = r-1, c-1
uf.Unite(r, c+n)
S = set()
for i in range(n+m):
S.add(uf.Find(i))
print(len(S)-1)
``` | output | 1 | 72,483 | 3 | 144,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,484 | 3 | 144,968 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
from sys import stdin
class DSU:
def __init__(self, n) -> None:
self.parent = [i for i in range(n)]
self.rank =[0]*n
def find_set(self, v):
w = v
parent = self.parent
while parent[v] != v:
v = parent[v]
while parent[w] != w:
t = parent[w]
parent[w] = v
w = t
return v
def union_sets(self, a, b):
a = self.find_set(a)
b = self.find_set(b)
rank = self.rank
if a != b:
if rank[a] < rank[b]:
rank[a], rank[b] = rank[b], rank[a]
self.parent[b] = a
if rank[a] == rank[b]:
rank[a] += 1
n,m,q=map(int,stdin.readline().split())
dsu=DSU(n+m)
for i in range(q):
r,c = map(int, stdin.readline().split())
r-=1
c-=1
dsu.union_sets(r, c+n)
ans = -1
for i in range(n+m):
if dsu.parent[i] == i:
ans += 1
print(ans)
``` | output | 1 | 72,484 | 3 | 144,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,485 | 3 | 144,970 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
"""
https://codeforces.com/contest/1012/problem/B
"""
from sys import stdin, stdout
class UnionFind():
def __init__(self, arr=[]):
self.rank = {}
self.leader = {}
for i in arr:
self.rank[i] = 0
self.leader[i] = i
def __repr__(self):
return f""" Ranks : {self.rank}, leaders : {self.leader} """
def find(self,x):
leader = self.leader.get(x, x)
if leader != x:
leader = self.find(leader)
self.leader[x] = leader
return leader
def getRank(self,x):
return self.rank.get( x, 0)
def union(self,x,y):
leader1 = self.find(x)
leader2 = self.find(y)
if leader1 == leader2 : return False
if self.getRank(leader1) > self.getRank(leader2):
self.leader[leader2] = leader1
elif self.getRank(leader1) < self.getRank(leader2):
self.leader[leader1] = leader2
else:
self.leader[leader1] = leader2
self.rank[leader2] = 1 + self.getRank(leader2)
return True
n,m,q = tuple(map(int,stdin.readline().split()))
uf = UnionFind( range(1,n+m+1) )
rcs = [ tuple(map(int,line.split())) for line in stdin ]
grps = n+m
for r,c in rcs:
if uf.union( r, c+n ):
grps-=1
stdout.write(str(grps-1))
``` | output | 1 | 72,485 | 3 | 144,971 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image> | instruction | 0 | 72,486 | 3 | 144,972 |
Tags: constructive algorithms, dfs and similar, dsu, graphs, matrices
Correct Solution:
```
from collections import deque
import os
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
n,m,q = map(int,input().split())
mn = m + n
cr = [[] for i in range(mn)]
ca = [-1]*(mn)
for i in range(q):
ri,ci = map(int,input().split())
ri -= 1
ci -= 1
cr[ci] += [ri+m]
cr[ri+m] += [ci]
ans = -1
for i in range(mn):
if ca[i] != -1:
continue
ca[i] = 1
stack = deque()
for j in cr[i]:
stack.append(j)
ca[j] = 0
while len(stack) != 0:
temp = stack.pop()
for j in cr[temp]:
if ca[j] == -1:
ca[j] = 0
stack.append(j)
ans += 1
print(ans)
``` | output | 1 | 72,486 | 3 | 144,973 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
import sys
ioRead = sys.stdin.readline
ioWrite = lambda x: sys.stdout.write(f"{x}\n")
n,m,q = map(int, ioRead().split(" "))
rows_and_columns = n + m
seen = [False for _ in range(rows_and_columns)]
connectsTo = [[] for _ in range(rows_and_columns)]
for _ in range(q):
r,c = map(lambda x: int(x) - 1, ioRead().split(" "))
c+= n
connectsTo[r].append(c)
connectsTo[c].append(r)
inserts = -1
for i in range(rows_and_columns):
if not seen[i]:
inserts +=1
stack = [i]
while stack: #BFS
current = stack.pop()
if not seen[current]:
seen[current] = True
for n in connectsTo[current]:
if not seen[n]:
stack.append(n)
ioWrite(inserts)
``` | instruction | 0 | 72,487 | 3 | 144,974 |
Yes | output | 1 | 72,487 | 3 | 144,975 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
from sys import stdin, stdout
class DSU:
def __init__(self, n):
self.root = [ -1 for x in range(0, n) ]
def find(self, i):
if self.root[i] == -1:
return i
else:
self.root[i] = self.find(self.root[i])
return self.root[i]
def link(self, i, j):
if self.find(i) == self.find(j):
return False
self.root[self.find(i)] = self.find(j)
return True
n, m, q = map(int, stdin.readline().split())
dsu = DSU(n+m+1)
groups = n+m
for line in stdin:
r, c = map(int, line.split())
groups -= dsu.link(r, n+c)
stdout.write(str(groups-1) + '\n')
``` | instruction | 0 | 72,488 | 3 | 144,976 |
Yes | output | 1 | 72,488 | 3 | 144,977 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
from sys import stdin
def main():
n, m, q = map(int, input().split())
chm = list(range(n + m))
r = [0] * (n + m)
n -= 1
res = n + m
for s in stdin.read().splitlines():
a, b = map(int, s.split())
a -= 1
b += n
l = []
while a != chm[a]:
l.append(a)
a = chm[a]
for c in l:
chm[c] = a
l = []
while b != chm[b]:
l.append(b)
b = chm[b]
for c in l:
chm[c] = b
if a != b:
if r[a] < r[b]:
chm[a] = b
elif r[b] < r[a]:
chm[b] = a
else:
chm[a] = b
r[b] += 1
res -= 1
print(res)
if __name__ == '__main__':
main()
``` | instruction | 0 | 72,489 | 3 | 144,978 |
Yes | output | 1 | 72,489 | 3 | 144,979 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
# vars: a, b, c, i, m, n, r, res, q, used, used_a, used_b
from array import array
def main():
n, m, q = map(int, input().split())
arr1 = array("I", (1,))
''' :type: list '''
b = [array("I", arr1) for _ in range(n+3)]
a = [array("I", arr1) for __ in range(m+3)]
used_b = arr1*(n+1)
used_a = arr1*(m+1)
res = m+1
for ___ in range(q):
r, c = map(int, input().split())
if used_b[r] == 1:
b[r] = [c]
used_b[r] = 2
else:
b[r].append(c)
if used_a[c] == 1:
a[c] = [r]
res -= 1
used_a[c] = 2
else:
a[c].append(r)
used_b1 = arr1*(n+1)
used_a1 = arr1*(m+1)
i = 1
while i <= n:
if used_b1[i] == 1:
queue = array("I", (i,))
for r in queue:
for c in b[r]:
if used_a1[c] == 1:
used_a1[c] = 2
for r2 in a[c]:
if used_b1[r2] == 1:
used_b1[r2] = 2
queue.append(r2)
res += 1
i += 1
if res == 2:
print('0')
else:
print(res-2)
main()
``` | instruction | 0 | 72,490 | 3 | 144,980 |
No | output | 1 | 72,490 | 3 | 144,981 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
import sys
import os
def traverse(edges, start):
visited = set()
q = [(start, 0)]
while len(q) > 0:
t = q[len(q) - 1]
node = t[0]
idx = t[1]
newnode = edges[node][idx]
if newnode not in visited:
visited.add(newnode)
q.append((newnode, 0))
else:
q.pop()
if idx < len(edges[node]) - 1:
q.append((node, idx + 1))
return visited
def count(edges):
visited = set()
result = 0
for k, v in edges.items():
if k not in visited:
result += 1
visited.update(traverse(edges, k))
return result
def solve(n, m, have):
have.sort()
xdict = dict()
xocc = set()
yocc = set()
for i in range(len(have)):
x = have[i][0]
y = have[i][1]
if x in xdict:
xdict[x].append(y)
else:
xdict[x] = [y]
xocc.add(x)
yocc.add(y)
edges = dict()
for k, v in xdict.items():
for i in range(len(v) - 1):
if v[i] in edges:
edges[v[i]].append(v[i + 1])
else:
edges[v[i]] = [v[i + 1]]
if v[i + 1] in edges:
edges[v[i + 1]].append(v[i])
else:
edges[v[i + 1]] = [v[i]]
if len(xocc) == 0 and len(yocc) == 0:
return n + m - 1
return count(edges) + n + m - len(xocc) - len(yocc)
def main():
n, m, q = map(int, input().split())
have = []
for i in range(q):
x, y = map(int, input().split())
have.append((x, y))
print(solve(n, m, have))
if __name__ == '__main__':
main()
``` | instruction | 0 | 72,491 | 3 | 144,982 |
No | output | 1 | 72,491 | 3 | 144,983 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
x = set()
y = set()
n, m, q = map(int, input().split())
for i in range(0, q):
r, c = map(int, input().split())
x.add(r)
y.add(c)
a = len(x)
b = len(y)
if a == q or b == q:
print(n-a + m-b + 1)
else:
print(n-a + m-b)
``` | instruction | 0 | 72,492 | 3 | 144,984 |
No | output | 1 | 72,492 | 3 | 144,985 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.
Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).
<image>
Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.
Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.
Input
The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.
The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.
Output
Print the minimal number of elements to be purchased.
Examples
Input
2 2 3
1 2
2 2
2 1
Output
0
Input
1 5 3
1 3
1 1
1 5
Output
2
Input
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
Output
1
Note
For each example you have a picture which illustrates it.
The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.
The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.
Test 1
We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything.
<image>
Test 2
We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.
<image>
Test 3
There are several possible solutions. One of them is illustrated below.
Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions.
<image>
Submitted Solution:
```
import re
in1 = [int(x) for x in re.split("\\s", input())]
nodes1 = [[] for x in range(in1[0])]
node1C = in1[0]
nodes2 = [[] for x in range(in1[1])]
node2C = in1[1]
nodes3 = in1[2]
for x in range(nodes3):
inX = [int(x)-1 for x in re.split("\\s", input())]
nodes1[inX[0]].append(inX[1])
nodes2[inX[1]].append(inX[0])
reset = True
while reset:
reset = False
for x in range(in1[0]):
if len(nodes1[x]) > 1 and len(nodes1[x]) < node1C:
nodes1[x] = [y for y in range(node2C)]
for y in range(node2C):
if x not in nodes2[y]:
nodes2[y].append(x)
for x in range(in1[1]):
if len(nodes2[x]) > 1 and len(nodes2[x]) < node2C:
nodes2[x] = [y for y in range(node2C)]
for y in range(node1C):
if x not in nodes1[y]:
nodes1[y].append(x)
reset = True
count = 0
for x in range(in1[0]):
if not nodes1[x]:
count += 1
for x in range(in1[1]):
if not nodes2[x]:
count += 1
print(count)
``` | instruction | 0 | 72,493 | 3 | 144,986 |
No | output | 1 | 72,493 | 3 | 144,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
pi = 3.141592653589793238462643383279502884197169399375105820974
n, px, py = [int(i) for i in input().split(' ')]
p = [[int(i) for i in input().split(' ')] for i in range(n)]
p.append(p[0])
b = (p[0][0] - px)**2 + (p[0][1] - py)**2
Min, Max = b, b
for i in range(1,len(p)):
a = (p[i][0] - p[i-1][0])**2 + (p[i][1] - p[i-1][1])**2
c = b
b = (p[i][0] - px)**2 + (p[i][1] - py)**2
an = max(b - a - c, c - a - b)
if an >= 0:
if b < Min:
Min = b
else:
tmp = (p[i][0] - px) * (p[i-1][1] - py) - (p[i-1][0] - px) * (p[i][1] - py)
tmp = tmp**2 / a
if tmp < Min:
Min = tmp
if b > Max:
Max = b
print('%.18f'%((Max-Min)*pi))
``` | instruction | 0 | 72,925 | 3 | 145,850 |
Yes | output | 1 | 72,925 | 3 | 145,851 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 missingdays <missingdays@missingdays>
#
# Distributed under terms of the MIT license.
"""
"""
import math
def pythagoras(p, p1):
return math.sqrt(pow(p[0]-p1[0], 2) + pow(p[1]-p1[1], 2))
def distance(p, p1, p2):
if p2[1] == p1[1]:
if p[0] < min(p1[0], p2[0]) or p[0] > max(p1[0], p2[0]):
return pythagoras(p, p1)
else:
return abs(p1[1] - p[1])
if p2[0] == p1[0]:
if p[1] < min(p1[1], p2[1]) or p[1] > max(p1[1], p2[1]):
return pythagoras(p, p1)
else:
return abs(p1[0] - p[0])
k = (p2[1] - p1[1]) / (p2[0] - p1[0])
b = p2[1] - k*p2[0]
k0 = -1/k
b0 = p[1] - k0*p[0]
x = (b-b0) / (k0 - k)
y = k*x + b
if x < min(p1[0], p2[0]) or x > max(p1[0], p2[0]):
return pythagoras(p, p1)
else:
return math.sqrt(pow(p[0]-x, 2) + pow(p[1]-y, 2))
def calc(v, p):
m = float("inf")
for i in range(len(v)-1):
m = min(m, distance(p, v[i], v[i+1]))
m = min(m, distance(p, v[0], v[len(v)-1]))
return m
n, x, y = [int(i) for i in input().split()]
v = []
sr = 0
br = 0
for i in range(n):
a, b = [int(k) for k in input().split()]
v.append([a, b])
r = math.sqrt(pow(a-x, 2) + pow(b-y , 2))
br = max(br, r)
sr = calc(v, [x, y])
print(math.pi * (pow(br, 2) - pow(sr, 2)))
``` | instruction | 0 | 72,926 | 3 | 145,852 |
Yes | output | 1 | 72,926 | 3 | 145,853 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
import math
n,a,b=map(int,input().split())
m=[]
s=[]
for i in range(n):
m.append(list(map(int,input().split())))
for i in range(n):
a1,b1=m[i]
s.append((a1-a)**2+(b1-b)**2)
for i in range(n-1):
a1,b1=m[i]
a2,b2=m[i+1]
if ((a2-a1)*(a2-a)+(b2-b1)*(b2-b))*((a2-a1)*(a1-a)+(b2-b1)*(b1-b))<=0:
l=((b2-b1)*a-(a2-a1)*b-a1*b2+a2*b1)**2/((b2-b1)**2+(a2-a1)**2)
s.append(l)
a1,b1=m[-1]
a2,b2=m[0]
if ((a2-a1)*(a2-a)+(b2-b1)*(b2-b))*((a2-a1)*(a1-a)+(b2-b1)*(b1-b))<=0:
l=((b2-b1)*a-(a2-a1)*b-a1*b2+a2*b1)**2/((b2-b1)**2+(a2-a1)**2)
s.append(l)
t=math.pi*(max(s)-min(s))
print(t)
``` | instruction | 0 | 72,927 | 3 | 145,854 |
Yes | output | 1 | 72,927 | 3 | 145,855 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
import math
def Len (w):
return w[0] * w[0] + w[1] * w[1]
n, x, y = input().split(' ')
n = int(n)
x = int(x)
y = int(y)
arr = []
for i in range(0, n):
a, b = input().split(' ')
a = int(a)
b = int(b)
arr.append([a, b])
r1 = 5000000000000
r2 = -500000000000
for i in range(0, n):
st = [arr[(i + 1) % n][0] - arr[i][0], arr[(i + 1) % n][1] - arr[i][1]]
r = [arr[i][0] - x, arr[i][1] - y]
q = [arr[(i + 1) % n][0] - x, arr[(i + 1) % n][1] - y]
d = q[0] * r[1] - q[1] * r[0]
d *= d
s = d/Len(st)
if Len (q) - s < Len (st) and Len(r) - s < Len(st):
r1 = min(r1, s)
r2 = max(r2, s)
for i in arr:
s = (x - i[0]) * (x - i[0]) + (y - i[1]) * (y - i[1])
r1 = min(r1, s)
r2 = max(r2, s)
print ((r2 - r1) * math.pi)
``` | instruction | 0 | 72,928 | 3 | 145,856 |
Yes | output | 1 | 72,928 | 3 | 145,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
import math
n, x, y = [int(x) for x in input().split()]
firstx, firsty = [int(z) for z in input().split()]
rem1, rem2 = firstx, firsty
diff = math.sqrt((x-firstx)**2 + (y-firsty)**2)
radius_max = diff
radius_min = diff
for _ in range(n-1):
nx, ny = [int(z) for z in input().split()]
diff = math.sqrt((x-nx)**2 + (y-ny)**2)
radius_max = max(radius_max, diff)
radius_min = min(radius_min, diff)
diff = math.sqrt( (x-((nx+firstx)/2))**2 + (y-((ny+firsty)/2))**2)
radius_max = max(radius_max, diff)
radius_min = min(radius_min, diff)
firstx = nx
firsty = ny
diff = math.sqrt((firstx-rem1)**2 + (firsty-rem2)**2)
radius_max = max(radius_max, diff)
radius_min = min(radius_min, diff)
print(math.pi*radius_max**2 - math.pi*radius_min**2)
``` | instruction | 0 | 72,929 | 3 | 145,858 |
No | output | 1 | 72,929 | 3 | 145,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
import math
n, px, py = map(int, input().split())
maxdist = 0
mindist = 10000000000000
for i in range(n):
x, y = map(int, input().split())
d = (px - x)**2 + (py - y)**2
if d > maxdist:
maxdist = d
if d < mindist:
mindist = d
print( math.pi * (maxdist - mindist) )
``` | instruction | 0 | 72,931 | 3 | 145,862 |
No | output | 1 | 72,931 | 3 | 145,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.
Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.
Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.
Input
The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.
Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.
All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.
Output
Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3 0 0
0 1
-1 2
1 2
Output
12.566370614359172464
Input
4 1 -1
0 0
1 2
2 0
1 1
Output
21.991148575128551812
Note
In the first sample snow will be removed from that area:
<image>
Submitted Solution:
```
import math
def radius(ax, ay, bx, by):
return math.sqrt((ax-bx)**2+(ay-by)**2)
def S(ax, ay, bx, by):
x1 = ax-px
x2 = bx-px
y1 = ay-py
y2 = by-py
return(abs((x1*x2-y2*y1)/2))
def height(ax, ay, bx, by):
s = S(ax, ay, bx, by)
r = radius(ax, ay, bx, by)
h = (2*s)/r
return h
n, px, py = map(int, input().split())
rs = 3000000000
rb = 0
x, y = map(int, input().split())
r = radius(x, y, px, py)
if rs >= r:
rs = r
if rb <= r:
rb = r
xf, yf = x,y
x1, y1 = x,y
for i in range(n-1):
x, y = map(int, input().split())
r = radius(x, y, px, py)
if rs >= r:
rs = r
if rb <= r:
rb = r
h = height(x, y, x1, y1)
if rs >= h:
rs = h
x1, y1 = x, y
h = height(x, y, xf, yf)
if rs >= h:
rs = h
print(math.pi*(rb**2-rs**2))
``` | instruction | 0 | 72,932 | 3 | 145,864 |
No | output | 1 | 72,932 | 3 | 145,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,054 | 3 | 146,108 |
Tags: greedy, sortings
Correct Solution:
```
from bisect import *
from collections import defaultdict
n,k = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
hash = defaultdict(int)
for i in l:
hash[i]+=1
la = list(set(l))
la.sort()
count = 0
# print(la)
yo = set()
for j,i in enumerate(la):
z1 = len(la)-j-1
if z1>0:
if i+k in hash:
yo.add(j)
count+=1
continue
else:
z2 = bisect_right(la,i+k)
if z2>j+1 :
yo.add(j)
count+=1
ans = 0
for i in range(len(la)):
if i not in yo:
ans+=hash[la[i]]
print(ans)
``` | output | 1 | 73,054 | 3 | 146,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,055 | 3 | 146,110 |
Tags: greedy, sortings
Correct Solution:
```
line = list(map(int,input().split()))
n = line[0]
k = line[1]
a = list(map(int,input().split()))
freq = {}
for x in a:
if x in freq:
freq[x] += 1
else:
freq[x] = 1
a = list(set(a)) #remove dupes
a.sort()
#print(str(a))
#print(str(freq))
for i in range(len(a)-1):
if a[i+1] <= a[i]+k:
n -= freq[a[i]]
print(str(n))
``` | output | 1 | 73,055 | 3 | 146,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,056 | 3 | 146,112 |
Tags: greedy, sortings
Correct Solution:
```
if __name__ == "__main__":
n, K= map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cnt = 0
c = 1
for i in range(1,n):
if a[i]==a[i-1]:
c+=1
elif a[i]<=a[i-1]+K:
cnt+=c
c=1
else: c= 1
print(n-cnt)
``` | output | 1 | 73,056 | 3 | 146,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,057 | 3 | 146,114 |
Tags: greedy, sortings
Correct Solution:
```
from collections import Counter
I = lambda: map(int, input().split())
_, K = I()
C = Counter(I())
prev = 0
for a in sorted(C):
if a - prev <= K:
C[prev] = 0
prev = a
print(sum(C.values()))
``` | output | 1 | 73,057 | 3 | 146,115 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,058 | 3 | 146,116 |
Tags: greedy, sortings
Correct Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d={}
for i in l:
try:
d[i]+=1
except:
d.update({i:0})
d[i]+=1
i,j,c=1,0,0
while True:
if i>=n or j>=n:
break
if l[i]>l[j] and l[i]<=l[j]+k:
c+=d[l[j]]
i+=1
j+=1
elif l[i]==l[j]:
i+=1
j+=1
else:
i+=1
j+=1
print(n-c)
``` | output | 1 | 73,058 | 3 | 146,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,059 | 3 | 146,118 |
Tags: greedy, sortings
Correct Solution:
```
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = 1
i = 1
while i < len(a):
if a[i - 1] > a[i]:
while i < len(a) and a[i] + k >= a[i - 1]:
i += 1
i += 1
if i - 1 < len(a):
ans += 1
print(ans)
``` | output | 1 | 73,059 | 3 | 146,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,060 | 3 | 146,120 |
Tags: greedy, sortings
Correct Solution:
```
n, k = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
y = 0
d=0
for i in range(n-1):
if x[i+1]>x[i] and x[i+1]<=x[i]+k:
y+=1+d
d=0
if x[i+1]==x[i]:
d+=1
if x[i+1]!= x[i]:
d=0
print(n-y)
``` | output | 1 | 73,060 | 3 | 146,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria. | instruction | 0 | 73,061 | 3 | 146,122 |
Tags: greedy, sortings
Correct Solution:
```
def main():
n, k = map(int, input().split())
content= sorted(map(int, input().split()))
double = 0
answer = 1
if len(content) == 1:
print(1)
return
j = 1
while j < len(content):
if content[j] > content[j - 1]:
if content[j] > content[j - 1] + k:
answer += double + 1
double = 0
elif content[j] == content[j - 1]:
double += 1
j += 1
if (double):
answer += double
print(answer)
main()
``` | output | 1 | 73,061 | 3 | 146,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
stfn, stfk = input().split()
n,K = int(stfn), int(stfk)
array = [int(s) for s in input().split()]
array = sorted(array)
hmap = dict()
ar = [array[0]]
for j in array:
if j != ar[-1]:
ar.append(j)
if j in hmap:
hmap[j] += 1
else:
hmap[j] = 1
arr = []
i = 0
while i < len(ar) - 1:
if not (ar[i+1] > ar[i] and ar[i+1] <= ar[i]+K):
arr.append(ar[i])
i += 1
arr.append(ar[-1])
additLength = 0
for j in arr:
if j in hmap:
additLength += (hmap[j]-1)
print(len(arr) + additLength)
``` | instruction | 0 | 73,062 | 3 | 146,124 |
Yes | output | 1 | 73,062 | 3 | 146,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
def binarySearch(n,i,k,a):
l=i+1
u=n-1
ans=0
while l<=u:
mid=int((l+u)/2)
if a[mid]==a[i] :
l=mid+1
elif a[mid]-a[i]<=k:
ans=1
break
else:
u=mid-1
return ans
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
#print(str(a))
visited=[]
for i in range(0,n):
visited.append(0)
i=0
j=1
ctr=0
for i in range (0,n):
val=binarySearch(n,i,k,a)
ctr+=val
# print(str(visited))
print(n-ctr)
``` | instruction | 0 | 73,063 | 3 | 146,126 |
Yes | output | 1 | 73,063 | 3 | 146,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
n, m = map(int, input().split())
l, j = sorted(map(int, input().split())), 0
for a in l:
while l[j] < a:
if a <= l[j] + m:
n -= 1
j += 1
print(n)
``` | instruction | 0 | 73,064 | 3 | 146,128 |
Yes | output | 1 | 73,064 | 3 | 146,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
from collections import Counter
def i_ints():
return list(map(int, input().split()))
n, K = i_ints()
a = i_ints()
a = sorted(Counter(a).items())
a.append((a[-1][0] + K + 1, 0))
count = 0
for i in range(len(a) - 1):
if a[i][0] + K < a[i + 1][0]:
count += a[i][1]
print(count)
``` | instruction | 0 | 73,065 | 3 | 146,130 |
Yes | output | 1 | 73,065 | 3 | 146,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
def check(a):
flag = True
if len(a) == 1:
return False
for i in range(len(a) - 1):
if a[i] < a[i + 1] and a[i] + k >= a[i + 1]:
return True
else:
flag = False
return flag
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
while check(a):
for i in range(len(a) - 1):
if a[i] < a[i + 1] and a[i] + k >= a[i + 1]:
a[i] = -178797
# print(a)
a = list(filter(lambda x: x != -178797, a))
if len(a) == 1:
break
print(len(a))
``` | instruction | 0 | 73,066 | 3 | 146,132 |
No | output | 1 | 73,066 | 3 | 146,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
line1 = input().split()
n = int(line1[0])
k = int(line1[1])
nr = []
b = []
count = 0
line2 = input().split()
for i in line2:
nr.append(int(i))
b.append(bool(1))
nr = sorted(nr)
i = 1
while i < n:
if nr[i] - nr[i-1] <= k and nr[i] > nr[i-1]:
b[i-1] = bool(0)
i += 1
i = n - 1
while i > 0:
while (i > 0) and (b[i]):
i -= 1
j = i
while (j >= 0) and nr[j] == nr[i]:
b[j] = bool(0)
j -= 1
i = j
i = 0
while i < n:
if (b[i]):
count += 1
i += 1
print(count)
``` | instruction | 0 | 73,067 | 3 | 146,134 |
No | output | 1 | 73,067 | 3 | 146,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a, reverse = True)
eat = [k] * n
i = 1
current = 0
l = len(a)
ans = 1
while (current < l) and (i < l):
if a[i] < a[current] <= a[i] + eat[current]:
eat[current] += (a[current] - a[i])
i += 1
else:
i += 1
current += i
ans += 1
print(ans)
#print(lines)
#f = [True] * n
#ans = 0
#for i in range(n):
# if f[i]:
# ans += 1
# q = [i]
# head = 0
# while head != len(q):
# for j in lines[q[head]]:
# if f[j]:
# f[j] = False
# q.append(j)
# head += 1
# #print(q)
#print(ans)
``` | instruction | 0 | 73,068 | 3 | 146,136 |
No | output | 1 | 73,068 | 3 | 146,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if a_i > a_j and a_i ≤ a_j + K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if a_i > a_j and a_i ≤ a_j + K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101, 53, 42, 102, 101, 55, 54] and K=1. The one of possible sequences of swallows is: [101, 53, 42, 102, \underline{101}, 55, 54] → [101, \underline{53}, 42, 102, 55, 54] → [\underline{101}, 42, 102, 55, 54] → [42, 102, 55, \underline{54}] → [42, 102, 55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
Input
The first line contains two space separated positive integers n and K (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ K ≤ 10^6) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) — sizes of bacteria you have.
Output
Print the only integer — minimal possible number of bacteria can remain.
Examples
Input
7 1
101 53 42 102 101 55 54
Output
3
Input
6 5
20 15 10 15 20 25
Output
1
Input
7 1000000
1 1 1 1 1 1 1
Output
7
Note
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20, 15, 10, 15, \underline{20}, 25] → [20, 15, 10, \underline{15}, 25] → [20, 15, \underline{10}, 25] → [20, \underline{15}, 25] → [\underline{20}, 25] → [25].
In the third example no bacteria can swallow any other bacteria.
Submitted Solution:
```
n,k = input().split()
n = int(n)
k = int(k)
a = list(map(int,input().split()))
a.sort()
i = 0
j = len(a)-1
while(True):
if(i == len(a)-1):
break
elif(j == i):
i = i+1
j = len(a)-1
elif(a[j] <= a[i]+k):
a.remove(a[i])
print(a)
j = len(a)-1
else:
j = j-1
print(len(a))
``` | instruction | 0 | 73,069 | 3 | 146,138 |
No | output | 1 | 73,069 | 3 | 146,139 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,151 | 3 | 146,302 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
def main():
def find(x):
if par[x] < 0:
return x
else:
px = find(par[x])
wei[x] += wei[par[x]]
par[x] = px
return px
def weight(x):
find(x)
return wei[x]
def unite(x, y, w):
w += wei[x] - wei[y]
x = find(x)
y = find(y)
if x == y:
return False
else:
# sizeの大きいほうがx
if par[x] > par[y]:
x, y = y, x
w = -w
par[x] += par[y]
par[y] = x
wei[y] = w
return True
def same(x, y):
return find(x) == find(y)
def size(x):
return -par[find(x)]
def diff(x, y):
return weight(y) - weight(x)
n, m = map(int, input().split())
# 初期化
par = [-1] * n
wei = [0] * n
for _ in range(m):
l, r, d = map(int, input().split())
l -= 1
r -= 1
if same(l, r):
if diff(l, r) != d:
print('No')
sys.exit()
else:
unite(l, r, d)
print('Yes')
if __name__ == '__main__':
main()
``` | output | 1 | 73,151 | 3 | 146,303 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,152 | 3 | 146,304 |
"Correct Solution:
```
def find(x):
'''
xの根を求める
'''
if par[x] < 0:
return x
else:
p = find(par[x])
diff_w[x] += diff_w[par[x]]
par[x] = p
return p
def weight(x):
'''
xの根からの距離を求める
'''
find(x)
return diff_w[x]
def union(x, y, w):
'''
w[y] = w[x] + w となるようにxとyの属する集合を併合する
'''
w += diff_w[x] - diff_w[y]
x = find(x)
y = find(y)
if x == y:
return False
if par[x] > par[y]:
x, y = y, x
w = -w
par[x] += par[y]
par[y] = x
diff_w[y] = w
return True
def same(x, y):
'''
xとyが同じ集合に属するかを判定する
'''
return find(x) == find(y)
def size(x):
'''
xが属する集合の個数を求める
'''
return -par[find(x)]
def diff(x, y):
'''
xとyが同じ集合に属するときの w[y] - w[x] を求める
'''
return weight(y) - weight(x)
n, m = map(int, input().split())
par = [-1] * n
diff_w = [0] * n
for _ in range(m):
l, r, d = map(int, input().split())
if same(l-1, r-1):
if diff(l-1, r-1) != d:
print('No')
exit()
else:
union(l-1, r-1, d)
print('Yes')
``` | output | 1 | 73,152 | 3 | 146,305 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,153 | 3 | 146,306 |
"Correct Solution:
```
N,M = map(int,input().split())
A = [[] for i in range(N)]
for i in range(M) :
l,r,d = map(int,input().split())
l -= 1
r -= 1
A[l].append([r,d])
A[r].append([l,-d])
ans = "Yes"
distance = ["#" for i in range(N)]
flg = 0
for i in range(N) :
if flg :
break
if distance[i] != "#" :
continue
distance[i] = 0
stack = [i]
while stack :
now = stack.pop()
for to,d in A[now] :
if distance[to] == "#" :
distance[to] = distance[now] + d
stack.append(to)
elif distance[to] != distance[now] + d :
ans = "No"
stack = []
flg = 1
break
print(ans)
``` | output | 1 | 73,153 | 3 | 146,307 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,154 | 3 | 146,308 |
"Correct Solution:
```
#設定
import sys
input = sys.stdin.buffer.readline
#ライブラリインポート
from collections import defaultdict
#入力受け取り
def getlist():
return list(map(int, input().split()))
class WeightedUnionFind:
def __init__(self, n):
self.par = [i for i in range(n + 1)]
self.rank = [0] * (n + 1)
self.weight = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
if self.par[x] == x:
return x
else:
y = self.find(self.par[x])
self.weight[x] += self.weight[self.par[x]]
self.par[x] = y
return y
def same_check(self, x, y):
return self.find(x) == self.find(y)
def union(self, x, y, w):
rx = self.find(x)
ry = self.find(y)
if self.rank[rx] < self.rank[ry]:
if self.same_check(x, y) != True:
self.size[y] += self.size[x]
self.size[x] = 0
self.par[rx] = ry
self.weight[rx] = w - self.weight[x] + self.weight[y]
else:
if self.same_check(x, y) != True:
self.size[x] += self.size[y]
self.size[y] = 0
self.par[ry] = rx
self.weight[ry] = -w - self.weight[y] + self.weight[x]
if self.rank[rx] == self.rank[ry]:
self.rank[rx] += 1
def size(self, x):
x = self.find(x)
return self.size(x)
def diff(self, x, y):
if self.same_check(x, y) == True:
return self.weight[x] - self.weight[y]
else:
print("inf")
#処理内容
def main():
N, M = getlist()
judge = "Yes"
UF = WeightedUnionFind(N)
for i in range(M):
L, R, D = getlist()
if UF.same_check(L - 1, R - 1):
if UF.diff(L - 1, R - 1) != D:
judge = "No"
break
else:
UF.union(L - 1, R - 1, D)
print(judge)
if __name__ == '__main__':
main()
``` | output | 1 | 73,154 | 3 | 146,309 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,155 | 3 | 146,310 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(100000)
def check(i, xs, checked):
xi = xs[i]
children = set()
for j, d in links[i]:
if checked[j]:
continue
if j not in xs:
xs[j] = xi + d
elif xi + d != xs[j]:
return False
children.add(j)
checked[i] = True
for j in children:
if not check(j, xs, checked):
return False
return True
def solve():
checked = [False] * n
for i in range(n):
if not checked[i]:
xs = {}
xs[i] = 0
if not check(i, xs, checked):
return False
return True
n, m = map(int, input().split())
links = [set() for _ in range(n)]
for _ in range(m):
l, r, d = map(int, input().split())
l -= 1
r -= 1
links[l].add((r, d))
links[r].add((l, -d))
print('Yes' if solve() else 'No')
``` | output | 1 | 73,155 | 3 | 146,311 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,156 | 3 | 146,312 |
"Correct Solution:
```
import sys
n,m=map(int,input().split())
lrd=[list(map(int,input().split())) for i in range(m)]
x=["a"]*(n+1)
#iより右にいるデータ
R=[[] for i in range(n+1)]
L=[[] for i in range(n+1)]
for u in lrd:
l,r,d=u
R[l].append([r,d])
L[r].append([l,d])
for i in range(1,n+1):
if x[i]=="a":
x[i]=0
que=[i]
while que:
h=que.pop()
for u in R[h]:
r,d=u
if x[r]=="a":
x[r]=x[h]+d
que.append(r)
else:
if x[r]==x[h]+d:
continue
else:
print("No")
sys.exit()
for u in L[h]:
l,d=u
if x[l]=="a":
x[l]=x[h]-d
que.append(l)
else:
if x[l]==x[h]-d:
continue
else:
print("No")
sys.exit()
print("Yes")
``` | output | 1 | 73,156 | 3 | 146,313 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,157 | 3 | 146,314 |
"Correct Solution:
```
N, M = [int(_) for _ in input().split()]
LRD = [[int(_) for _ in input().split()] for _ in range(M)]
G = {}
for l, r, d in LRD:
l -= 1
r -= 1
#0-indexed
G[l] = G.get(l, {})
G[r] = G.get(r, {})
G[l][r] = d
G[r][l] = -d
INF = float('inf')
D = [INF] * N
for i in range(N):
if i in G and D[i] == INF:
D[i] = 0
table = []
while True:
for k, v in G[i].items():
if D[k] == INF:
table += [k]
D[k] = D[i] + v
elif D[k] != D[i] + v:
print('No')
exit()
if table:
i = table.pop()
else:
break
print('Yes')
``` | output | 1 | 73,157 | 3 | 146,315 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes | instruction | 0 | 73,158 | 3 | 146,316 |
"Correct Solution:
```
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
class WeightedUnionFind:
def __init__(self, N):
self.par = [i for i in range(N)]
self.rank = [0] * N
self.weight = [0] * N # 親までの距離
def find(self, x):
if self.par[x] == x:
return x
else:
r = self.find(self.par[x])
self.weight[x] += self.weight[self.par[x]]
self.par[x] = r
return r
def union(self, x, y, w):
rx = self.find(x)
ry = self.find(y)
if self.rank[rx] < self.rank[ry]:
self.par[rx] = ry
self.weight[rx] = w - self.weight[x] + self.weight[y]
else:
self.par[ry] = rx
self.weight[ry] = -w - self.weight[y] + self.weight[x]
if self.rank[rx] == self.rank[ry]:
self.rank[rx] += 1
def diff_w(self, x, y):
return self.weight[x] - self.weight[y]
def main():
N, M = map(int, readline().split())
m = map(int, read().split())
LRD = list(zip(m, m, m))
D = WeightedUnionFind(N)
for l, r, d in LRD:
l -= 1
r -= 1
D.union(l, r, d)
for l, r, d in LRD:
l -= 1
r -= 1
D.find(l)
D.find(r)
if d != D.diff_w(l, r):
print('No')
sys.exit()
print('Yes')
main()
``` | output | 1 | 73,158 | 3 | 146,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes
Submitted Solution:
```
class WeightedUnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1)
# 根への距離を管理
self.weight = [0] * (n+1)
# 検索
def find(self, x):
if self.par[x] == x:
return x
else:
y = self.find(self.par[x])
# 親への重みを追加しながら根まで走査
self.weight[x] += self.weight[self.par[x]]
self.par[x] = y
return y
# 併合
def union(self, x, y, w):
rx = self.find(x)
ry = self.find(y)
# xの木の高さ < yの木の高さ
if self.rank[rx] < self.rank[ry]:
self.par[rx] = ry
self.weight[rx] = w - self.weight[x] + self.weight[y]
# xの木の高さ ≧ yの木の高さ
else:
self.par[ry] = rx
self.weight[ry] = -w - self.weight[y] + self.weight[x]
# 木の高さが同じだった場合の処理
if self.rank[rx] == self.rank[ry]:
self.rank[rx] += 1
# 同じ集合に属するか
def same(self, x, y):
return self.find(x) == self.find(y)
# xからyへのコスト
def diff(self, x, y):
return self.weight[x] - self.weight[y]
n,m = map(int,input().split())
uf = WeightedUnionFind(n)
ans = True
for i in range(m):
l,r,d = map(int,input().split())
if uf.same(l,r)==False:
uf.union(l,r,d)
else:
if abs(uf.diff(l,r))!=d:
ans = False
break
print("Yes" if ans else "No")
``` | instruction | 0 | 73,159 | 3 | 146,318 |
Yes | output | 1 | 73,159 | 3 | 146,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes
Submitted Solution:
```
from collections import deque
import sys
sys.setrecursionlimit(10**7)
N,M=map(int,input().split())
Graph=[[] for i in range(N)]
for i in range(M):
L,R,D=map(int,input().split())
Graph[L-1].append((R-1,D))
Graph[R-1].append((L-1,-D))
visit=[False]*N
dist=[0]*N
for i in range(N):
if not visit[i]:
visit[i]=True
A=deque()
A.append(i)
while A:
x=A.pop()
for y,d in Graph[x]:
if visit[y] and (dist[y]!=d+dist[x]):
print('No')
exit()
elif not visit[y]:
visit[y]=True
A.append(y)
dist[y]=d+dist[x]
print('Yes')
``` | instruction | 0 | 73,160 | 3 | 146,320 |
Yes | output | 1 | 73,160 | 3 | 146,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes
Submitted Solution:
```
import sys
sys.setrecursionlimit(200002)
n, m = map(int, input().split())
a = [[] for i in range(n)]
for i in range(m):
l, r, d = map(int, input().split())
l -= 1
r -= 1
a[l].append((r, d))
a[r].append((l, -d))
bio = [None for i in range(n)]
def dfs(x):
for y, d in a[x]:
if bio[y] is None:
bio[y] = bio[x] + d
if not dfs(y):
return False
elif bio[y] != bio[x] + d:
return False
return True
for i in range(n):
if bio[i] is None:
bio[i] = 0
if not dfs(i):
print('No')
exit(0)
print('Yes')
``` | instruction | 0 | 73,161 | 3 | 146,322 |
Yes | output | 1 | 73,161 | 3 | 146,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import deque
# people on a line
n, m = map(int, input().split())
graph = {i: {} for i in range(1, n + 1)}
for _ in range(m):
a, b, c = map(int, input().split())
graph[a][b] = c
graph[b][a] = -c
distance = ["inf" for i in range(n + 1)]
# グラフにおいて順に点を決めてって、矛盾がないかどうかを調べる
visited = [False for i in range(n + 1)]
visited[0] = True
def find(x):
d = deque()
visited[x] = True
distance[x] = 0
d.append(x)
flag = True
while d and flag:
x = d.popleft()
for node in graph[x]:
if visited[node] == False:
visited[node] = True
d.append(node)
if distance[node] == "inf":
distance[node] = distance[x] + graph[x][node]
else:
if distance[node] != distance[x] + graph[x][node]:
flag = False
break
elif visited[node]==True:
if distance[node]!=distance[x]+graph[x][node]:
flag=False
break
if flag:
return False
else:
return True
all_flag = True
not_visited = True
while not_visited and all_flag:
judge = False
for i in range(1, n + 1):
if visited[i] == False:
judge = True
if find(i):
all_flag = False
break
else:
pass
if not judge:
not_visited = False
if all_flag:
print("Yes")
else:
print("No")
``` | instruction | 0 | 73,162 | 3 | 146,324 |
Yes | output | 1 | 73,162 | 3 | 146,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate.
You will given M pieces of information regarding the positions of these people. The i-th piece of information has the form (L_i, R_i, D_i). This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.
It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.
Constraints
* 1 \leq N \leq 100 000
* 0 \leq M \leq 200 000
* 1 \leq L_i, R_i \leq N (1 \leq i \leq M)
* 0 \leq D_i \leq 10 000 (1 \leq i \leq M)
* L_i \neq R_i (1 \leq i \leq M)
* If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).
* D_i are integers.
Input
Input is given from Standard Input in the following format:
N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M
Output
If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print `Yes`; if it does not exist, print `No`.
Examples
Input
3 3
1 2 1
2 3 1
1 3 2
Output
Yes
Input
3 3
1 2 1
2 3 1
1 3 5
Output
No
Input
4 3
2 1 1
2 3 5
3 4 2
Output
Yes
Input
10 3
8 7 100
7 9 100
9 8 100
Output
No
Input
100 0
Output
Yes
Submitted Solution:
```
import sys
from collections import deque
input = sys.stdin.readline
N, M = [int(x) for x in input().strip().split()]
L = [deque([]) for _ in range(N)]
for m in range(M):
l, r, d = [int(x) for x in input().strip().split()]
L[l-1].append((r, d))
L[r-1].append((l, -d))
D = [0] * N
F = [False] * N
def dfs(n, d):
if F[n-1]:
if D[n-1] == d:
return True
else:
return False
F[n-1] = True
D[n-1] = d
for nn, d_ in L[n-1]:
if not dfs(nn, d+d_):
return False
else:
return True
for n in range(1, N+1):
if not dfs(n, D[n-1]):
print('No')
exit()
else:
print('Yes')
``` | instruction | 0 | 73,163 | 3 | 146,326 |
No | output | 1 | 73,163 | 3 | 146,327 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.