problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02761 | Python | Runtime Error | N, M = map(int, input().split())
sc = [tuple(map(int, input().split())) for _ in range(M)]
for i in range(1000):
t = str(i)
if len(t) != N:
continue
ok = True
for s, c in sc:
if t[int(s)] != int(c):
ok = False
break
if ok:
print(i)
exit()
print(-1)
| N, M = map(int, input().split())
sc = [tuple(map(int, input().split())) for _ in range(M)]
for i in range(1000):
t = str(i)
if len(t) != N:
continue
ok = True
for s, c in sc:
if int(t[s - 1]) != c:
ok = False
break
if ok:
print(i)
exit()
print(-1)
| replace | 9 | 10 | 9 | 10 | 0 | |
p02761 | C++ | Runtime Error | /*
* Created by solzard on 2020-03-05.
* C - Guess The Number
*/
#include <iostream>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int GuessNumber(int n, int m, const std::vector<int> &s,
const std::vector<int> &c) {
int res = -1;
for (int num = 999; num >= 0; --num) {
std::string num_str = std::to_string(num);
bool is_ok = true;
if (num_str.size() != n)
continue;
rep(i, m) {
if (num_str[s[i] - 1] != (char)('0' + c[i])) {
is_ok = false;
break;
}
}
if (is_ok)
res = num;
}
return res;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int n, m;
std::vector<int> s, c;
std::cin >> n >> m;
rep(i, m) { std::cin >> s[i] >> c[i]; }
std::cout << GuessNumber(n, m, s, c) << "\n";
}
| /*
* Created by solzard on 2020-03-05.
* C - Guess The Number
*/
#include <iostream>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int GuessNumber(int n, int m, const std::vector<int> &s,
const std::vector<int> &c) {
int res = -1;
for (int num = 999; num >= 0; --num) {
std::string num_str = std::to_string(num);
bool is_ok = true;
if (num_str.size() != n)
continue;
rep(i, m) {
if (num_str[s[i] - 1] != (char)('0' + c[i])) {
is_ok = false;
break;
}
}
if (is_ok)
res = num;
}
return res;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int n, m;
std::vector<int> s(5, 0), c(5, 0);
std::cin >> n >> m;
rep(i, m) { std::cin >> s[i] >> c[i]; }
std::cout << GuessNumber(n, m, s, c) << "\n";
}
| replace | 34 | 35 | 34 | 35 | -11 | |
p02761 | C++ | Runtime Error | /*
URL https://
SCORE 0
AC false
WA false
TLE false
MLE false
TASK_TYPE
FAILURE_TYPE
NOTES
*/
#include <algorithm>
#include <cstdint>
#include <experimental/optional>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
/* import STL */
// stream
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::flush;
// basic types
using std::nullptr_t;
using std::pair;
using std::string;
using std::tuple;
using std::experimental::optional;
// function for basic types
using std::get;
using std::make_pair;
using std::make_tuple;
using std::experimental::make_optional;
/* TODO remove them */
using std::queue;
using std::stack;
using std::vector;
// algorithms
using std::accumulate;
using std::lower_bound;
using std::max_element;
using std::min_element;
using std::nth_element;
using std::upper_bound;
/* macros */
// loops
#define REP(i, n) for (i64 i = 0; i < static_cast<decltype(i)>(n); ++i)
#define REPR(i, n) for (i64 i = (n)-1; i >= static_cast<decltype(i)>(0); --i)
#define FOR(i, n, m) for (i64 i = (n); i < static_cast<decltype(i)>(m); ++i)
#define FORR(i, n, m) for (i64 i = (m)-1; i >= static_cast<decltype(i)>(n); --i)
#define EACH(x, xs) for (auto &x : (xs))
#define EACH_V(x, xs) for (auto x : (xs))
// helpers
#define CTR(x) (x).begin(), (x).end()
/* utils for std::tuple */
namespace internal {
namespace tuple_utils { // TODO rename to "internal::tuple"
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Tuple, size_t... Is>
void read(std::istream &stream, Tuple &t, seq<Is...>) {
static_cast<void>((int[]){0, (void(stream >> get<Is>(t)), 0)...});
}
template <class Tuple, size_t... Is>
void print(std::ostream &stream, Tuple const &t, seq<Is...>) {
static_cast<void>(
(int[]){0, (void(stream << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...});
}
template <size_t I, class F, class A, class... Elems> struct ForEach {
void operator()(A &arg, tuple<Elems...> const &t) const {
F()(arg, get<I>(t));
ForEach<I - 1, F, A, Elems...>()(arg, t);
}
void operator()(A &arg, tuple<Elems...> &t) const {
F()(arg, get<I>(t));
ForEach<I - 1, F, A, Elems...>()(arg, t);
}
};
template <class F, class A, class... Elems> struct ForEach<0, F, A, Elems...> {
void operator()(A &arg, tuple<Elems...> const &t) const {
F()(arg, get<0>(t));
}
void operator()(A &arg, tuple<Elems...> &t) const { F()(arg, get<0>(t)); }
};
template <class F, class A, class... Elems>
void for_each(A &arg, tuple<Elems...> const &t) {
ForEach<std::tuple_size<tuple<Elems...>>::value - 1, F, A, Elems...>()(arg,
t);
}
template <class F, class A, class... Elems>
void for_each(A &arg, tuple<Elems...> &t) {
ForEach<std::tuple_size<tuple<Elems...>>::value - 1, F, A, Elems...>()(arg,
t);
}
} // namespace tuple_utils
} // namespace internal
/* utils for Matrix (definition of Matrix) */
namespace internal {
namespace matrix {
template <typename V, int N> struct matrix_t {
using type = std::vector<typename matrix_t<V, N - 1>::type>;
};
template <typename V> struct matrix_t<V, 0> {
using type = V;
};
template <typename V, int N> using Matrix = typename matrix_t<V, N>::type;
template <typename V, typename It, int N> struct matrix_helper {
static Matrix<V, N> create(const It &begin, const It &end,
const V &default_value) {
return Matrix<V, N>(*begin, matrix_helper<V, It, N - 1>::create(
begin + 1, end, default_value));
}
};
template <typename V, typename It> struct matrix_helper<V, It, 0> {
static Matrix<V, 0> create(const It &begin __attribute__((unused)),
const It &end __attribute__((unused)),
const V &default_value) {
return default_value;
}
};
} // namespace matrix
} // namespace internal
/* Primitive types */
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using usize = size_t;
/* Data structure type */
template <typename T> using Vector = std::vector<T>;
template <typename V> using OrderedSet = std::set<V>;
template <typename V> using HashSet = std::unordered_set<V>;
template <typename K, typename V> using OrderedMap = std::map<K, V>;
template <typename K, typename V> using HashMap = std::unordered_map<K, V>;
template <typename T, int N> using Matrix = internal::matrix::Matrix<T, N>;
template <typename T, typename Compare = std::less<T>,
typename Container = std::vector<T>>
using PriorityQueue = std::priority_queue<T, Container, Compare>;
/* utils for Vector */
template <typename V> Vector<V> make_pre_allocated_vector(size_t N) {
Vector<V> retval;
retval.reserve(N);
return retval;
}
/* utils for Matrix */
template <class V, int N>
Matrix<V, N> make_matrix(const std::array<size_t, N> &shape,
V default_value = V()) {
return internal::matrix::matrix_helper<V, decltype(shape.begin()), N>::create(
shape.begin(), shape.end(), default_value);
}
/* utils for STL iterators */
namespace internal {
template <typename Iterator, typename F> struct MappedIterator {
MappedIterator(const Iterator &it, const F &function)
: it(it), function(function) {}
auto operator*() const { return this->function(this->it); }
void operator++() { ++this->it; }
void operator+=(size_t n) { this->it += n; }
auto operator+(size_t n) const {
return MappedIterator<Iterator, F>(this->it + n, this->function);
}
bool operator==(const MappedIterator<Iterator, F> &rhs) const {
return this->it == rhs.it;
}
bool operator!=(const MappedIterator<Iterator, F> &rhs) const {
return !(*this == rhs);
}
private:
Iterator it;
F function;
};
template <typename Iterator, typename P> struct FilteredIterator {
FilteredIterator(const Iterator &it, const Iterator &end, const P &predicate)
: it(it), end(end), predicate(predicate) {
if (this->it != end) {
if (!predicate(this->it)) {
this->increment();
}
}
}
decltype(auto) operator*() const { return *this->it; }
auto operator->() const { return this->it; }
void operator++() { this->increment(); }
void operator+=(size_t n) {
REP(i, n) { this->increment(); }
}
auto operator+(size_t n) const {
auto retval = *this;
retval += n;
return retval;
}
bool operator==(const FilteredIterator<Iterator, P> &rhs) const {
return this->it == rhs.it;
}
bool operator!=(const FilteredIterator<Iterator, P> &rhs) const {
return !(*this == rhs);
}
private:
void increment() {
if (this->it == this->end) {
return;
}
++this->it;
while (this->it != this->end && !this->predicate(this->it)) {
++this->it;
}
}
Iterator it;
Iterator end;
P predicate;
};
template <typename Iterator, typename ElementIterator>
struct FlattenedIterator {
FlattenedIterator(const Iterator &it, const Iterator &end)
: it(make_pair(it, ElementIterator())), end(end) {
if (this->it.first != this->end) {
this->it.second = it->begin();
}
this->find_valid();
}
decltype(auto) operator*() const { return this->it; }
const pair<Iterator, ElementIterator> *operator->() const {
return &this->it;
}
void operator++() { this->increment(); }
void operator+=(size_t n) {
REP(i, n) { this->increment(); }
}
auto operator+(size_t n) const {
auto retval = *this;
retval += n;
return retval;
}
bool
operator==(const FlattenedIterator<Iterator, ElementIterator> &rhs) const {
if (this->it.first != rhs.it.first) {
return false;
}
if (this->it.first == this->end || rhs.it.first == rhs.end) {
if (this->end == rhs.end) {
return true;
} else {
return false;
}
} else {
return this->it.second == rhs.it.second;
}
}
bool
operator!=(const FlattenedIterator<Iterator, ElementIterator> &rhs) const {
return !(*this == rhs);
}
private:
bool is_end() const { return this->it.first == this->end; }
bool is_valid() const {
if (this->is_end())
return false;
if (this->it.second == this->it.first->end())
return false;
return true;
}
void _increment() {
if (this->it.second == this->it.first->end()) {
++this->it.first;
if (this->it.first != this->end) {
this->it.second = this->it.first->begin();
}
} else {
++this->it.second;
}
}
void find_valid() {
while (!this->is_end() && !this->is_valid()) {
this->_increment();
}
}
void increment() {
this->_increment();
while (!this->is_end() && !this->is_valid()) {
this->_increment();
}
}
pair<Iterator, ElementIterator> it;
Iterator end;
};
} // namespace internal
template <class Iterator> struct Container {
Container(const Iterator &begin, const Iterator &end)
: m_begin(begin), m_end(end) {}
const Iterator &begin() const { return this->m_begin; }
const Iterator &end() const { return this->m_end; }
Iterator m_begin;
Iterator m_end;
};
template <typename C, typename F> auto iterator_map(const C &c, F function) {
using Iterator =
std::remove_const_t<std::remove_reference_t<decltype(c.begin())>>;
return Container<internal::MappedIterator<Iterator, F>>(
internal::MappedIterator<Iterator, F>(c.begin(), function),
internal::MappedIterator<Iterator, F>(c.end(), function));
}
template <typename C, typename P>
auto iterator_filter(const C &c, P predicate) {
using Iterator =
std::remove_const_t<std::remove_reference_t<decltype(c.begin())>>;
return Container<internal::FilteredIterator<Iterator, P>>(
internal::FilteredIterator<Iterator, P>(c.begin(), c.end(), predicate),
internal::FilteredIterator<Iterator, P>(c.end(), c.end(), predicate));
}
template <typename C> auto iterator_flatten(const C &c) {
using Iterator =
std::remove_const_t<std::remove_reference_t<decltype(c.begin())>>;
using ElementIterator = std::remove_const_t<
std::remove_reference_t<decltype(c.begin()->begin())>>;
return Container<internal::FlattenedIterator<Iterator, ElementIterator>>(
internal::FlattenedIterator<Iterator, ElementIterator>(c.begin(),
c.end()),
internal::FlattenedIterator<Iterator, ElementIterator>(c.end(), c.end()));
}
/* input */
template <class F, class S>
std::istream &operator>>(std::istream &stream, pair<F, S> &pair) {
stream >> pair.first;
stream >> pair.second;
return stream;
}
template <class... Args>
std::istream &operator>>(std::istream &stream, tuple<Args...> &tuple) {
internal::tuple_utils::read(
stream, tuple, internal::tuple_utils::gen_seq<sizeof...(Args)>());
return stream;
}
template <class T> T read() {
T t;
cin >> t;
return t;
}
template <class F, class S> pair<F, S> read() {
pair<F, S> p;
cin >> p;
return p;
}
template <class T1, class T2, class T3, class... Args>
tuple<T1, T2, T3, Args...> read() {
tuple<T1, T2, T3, Args...> t;
cin >> t;
return t;
}
template <typename T, typename F = std::function<T()>>
Vector<T> read(const usize length, F r) {
auto retval = make_pre_allocated_vector<T>(length);
REP(i, length) { retval.emplace_back(r()); }
return retval;
}
template <class T> Vector<T> read(const usize length) {
return read<T>(length, [] { return read<T>(); });
}
template <class F, class S> Vector<pair<F, S>> read(const usize length) {
return read<pair<F, S>>(length);
}
template <class T1, class T2, class T3, class... Args>
Vector<tuple<T1, T2, T3, Args...>> read(const usize length) {
return read<tuple<T1, T2, T3, Args...>>(length);
}
namespace internal {
template <typename T> struct oneline {
std::string operator()(const T &t) const {
std::ostringstream oss;
oss << t;
return oss.str();
}
};
template <typename F, typename S> struct oneline<pair<F, S>> {
std::string operator()(const pair<F, S> &p) const {
std::ostringstream oss;
oss << "{" << oneline<F>()(p.first) << ", " << oneline<S>()(p.second)
<< "}";
return oss.str();
}
};
template <typename... Args> struct oneline<tuple<Args...>> {
struct oneline_tuple {
template <class V>
void operator()(Vector<std::string> &strs, const V &v) const {
strs.emplace_back(oneline<V>()(v));
}
};
std::string operator()(const tuple<Args...> &t) const {
std::ostringstream oss;
Vector<std::string> strs;
internal::tuple_utils::for_each<oneline_tuple, Vector<std::string>,
Args...>(strs, t);
oss << "{";
REPR(i, strs.size()) {
oss << strs[i];
if (i != 0) {
oss << ", ";
}
}
oss << "}";
return oss.str();
}
};
template <> struct oneline<bool> {
std::string operator()(const bool b) const { return b ? "true" : "false"; }
};
template <typename X> struct oneline<Vector<X>> {
std::string operator()(const Vector<X> &vec) const {
std::string retval = "[";
auto f = oneline<X>();
REP(i, vec.size()) {
retval += f(vec[i]);
if (i != static_cast<i64>(vec.size() - 1)) {
retval += ", ";
}
}
retval += "]";
return retval;
}
};
template <typename X> struct oneline<OrderedSet<X>> {
std::string operator()(const OrderedSet<X> &s) const {
std::string retval = "{";
auto f = oneline<X>();
size_t ctr = 0;
EACH(x, s) {
retval += f(x);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename X> struct oneline<HashSet<X>> {
std::string operator()(const HashSet<X> &s) const {
std::string retval = "{";
auto f = oneline<X>();
size_t ctr = 0;
EACH(x, s) {
retval += f(x);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename K, typename V> struct oneline<OrderedMap<K, V>> {
std::string operator()(const OrderedMap<K, V> &s) const {
std::string retval = "{";
auto f1 = oneline<K>();
auto f2 = oneline<V>();
size_t ctr = 0;
EACH(x, s) {
retval += f1(x.first);
retval += ": ";
retval += f2(x.second);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename K, typename V> struct oneline<HashMap<K, V>> {
std::string operator()(const HashMap<K, V> &s) const {
std::string retval = "{";
auto f1 = oneline<K>();
auto f2 = oneline<V>();
size_t ctr = 0;
EACH(x, s) {
retval += f1(x.first);
retval += ": ";
retval += f2(x.second);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename V> struct keys { /* no implementation */
};
template <typename X> struct keys<std::vector<X>> {
Vector<size_t> operator()(const Vector<X> &v) const {
Vector<size_t> keys;
REP(i, v.size()) { keys.emplace_back(i); }
return keys;
}
};
template <typename K, typename V> struct keys<OrderedMap<K, V>> {
Vector<K> operator()(const OrderedMap<K, V> &c) const {
Vector<K> keys;
EACH(elem, c) { keys.emplace_back(elem.first); }
return keys;
}
};
template <typename K, typename V> struct keys<HashMap<K, V>> {
Vector<K> operator()(const HashMap<K, V> &c) const {
Vector<K> keys;
EACH(elem, c) { keys.emplace_back(elem.first); }
return keys;
}
};
} // namespace internal
template <typename T> void dump(const T &t) {
using namespace internal;
std::cerr << oneline<T>()(t) << std::endl;
}
template <typename V1, typename V2, typename... Args>
void dump(const V1 &v1, const V2 &v2, const Args &...args) {
using namespace internal;
using F = typename oneline<tuple<V1, V2, Args...>>::oneline_tuple;
auto x = std::make_tuple(v1, v2, args...);
Vector<std::string> strs;
internal::tuple_utils::for_each<F, Vector<std::string>, V1, V2, Args...>(strs,
x);
REPR(i, strs.size()) {
std::cerr << strs[i];
if (i != 0) {
std::cerr << ", ";
}
}
std::cerr << std::endl;
}
template <typename C> std::string as_set(const C &ctr) {
Vector<std::string> values;
using namespace internal;
EACH(x, ctr) { values.emplace_back(oneline<decltype(x)>()(x)); }
std::string retval = "---\n";
REP(i, values.size()) {
retval += values[i];
retval += "\n";
}
retval += "---";
return retval;
}
template <typename C> std::string as_map(const C &ctr) {
using namespace internal;
auto ks = keys<C>()(ctr);
Vector<std::string> keys;
Vector<std::string> values;
EACH(key, ks) {
keys.emplace_back(oneline<decltype(key)>()(key));
values.emplace_back(oneline<decltype(ctr.at(key))>()(ctr.at(key)));
}
size_t l = 0;
EACH(key, keys) { l = std::max(l, key.size()); }
std::string retval = "---\n";
REP(i, values.size()) {
retval += keys[i];
REP(j, l - keys[i].size()) { retval += " "; }
retval += ": ";
retval += values[i];
retval += "\n";
}
retval += "---";
return retval;
}
template <typename C> std::string as_table(const C &ctr) {
using namespace internal;
auto rkeys = keys<C>()(ctr);
auto ckeys = OrderedSet<std::string>();
auto values =
Vector<pair<std::string, OrderedMap<std::string, std::string>>>();
/* Stringify all data */
EACH(rkey, rkeys) {
auto rkey_str = oneline<decltype(rkey)>()(rkey);
values.emplace_back(rkey_str, OrderedMap<std::string, std::string>());
auto row = ctr.at(rkey);
auto ks = keys<decltype(row)>()(row);
EACH(ckey, ks) {
auto ckey_str = oneline<decltype(ckey)>()(ckey);
ckeys.emplace(ckey_str);
values.back().second.emplace(
ckey_str, oneline<decltype(row.at(ckey))>()(row.at(ckey)));
}
}
/* Calculate string length */
size_t max_row_key_length = 0;
EACH(value, values) {
max_row_key_length = std::max(max_row_key_length, value.first.size());
}
OrderedMap<std::string, size_t> max_col_length;
EACH(ckey, ckeys) { max_col_length.emplace(ckey, ckey.size()); }
EACH(value, values) {
EACH(elem, value.second) {
auto ckey = elem.first;
auto value = elem.second;
max_col_length[ckey] = std::max(max_col_length[ckey], value.size());
}
}
std::string retval = "---\n";
/* Header */
REP(i, max_row_key_length) { retval += " "; }
retval += " ";
size_t cnt = 0;
EACH(ckey, ckeys) {
retval += ckey;
REP(j, max_col_length[ckey] - ckey.size()) { retval += " "; }
cnt += 1;
if (cnt != ckeys.size()) {
retval += ", ";
}
}
retval += "\n------\n";
/* Values */
EACH(value, values) {
retval += value.first;
REP(i, max_row_key_length - value.first.size()) { retval += " "; }
retval += "| ";
size_t cnt = 0;
EACH(ckey, ckeys) {
auto v = std::string("");
if (value.second.find(ckey) != value.second.end()) {
v = value.second.at(ckey);
}
retval += v;
REP(j, max_col_length[ckey] - v.size()) { retval += " "; }
cnt += 1;
if (cnt != ckeys.size()) {
retval += ", ";
}
}
retval += "\n";
}
retval += "---";
return retval;
}
// Hash
namespace std {
template <class F, class S> struct hash<pair<F, S>> {
size_t operator()(const pair<F, S> &p) const {
return hash<F>()(p.first) ^ hash<S>()(p.second);
}
};
template <class... Args> struct hash<tuple<Args...>> {
struct hash_for_element {
template <class V> void operator()(size_t &size, const V &v) const {
size ^= std::hash<V>()(v);
}
};
size_t operator()(const tuple<Args...> &t) const {
size_t retval = 0;
internal::tuple_utils::for_each<hash_for_element, size_t, Args...>(retval,
t);
return retval;
}
};
} // namespace std
#define MAIN
void body();
// main function (DO NOT EDIT)
int main(int argc, char **argv) {
cin.tie(0);
std::ios_base::sync_with_stdio(false);
cout << std::fixed;
body();
return 0;
}
#ifndef MAIN
#include "common.cc"
#endif
static i64 mod(i64 a, i64 m) { return (a % m + m) % m; }
/*
* n^r
*/
template <class V> static V pow(V n, i64 r) {
if (r == 0) {
return 1;
}
auto r2 = r / 2;
auto x2 = pow(n, r2);
return x2 * x2 * ((r % 2 == 0) ? 1 : n);
}
static i64 gcd(i64 a, i64 b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <class Iterator>
static i64 gcd_ctr(const Iterator &begin, const Iterator &end) {
if (begin == end) {
return -1;
} else {
auto ans = *begin;
auto it = begin;
++it;
for (; it != end; ++it) {
auto x = *it;
ans = gcd(ans, x);
}
return ans;
}
}
static i64 gcd_ctr(const Vector<i64> &xs) {
return gcd_ctr(xs.begin(), xs.end());
}
/*
* a * get<0>(r) + b * get<1>(r) = get<2>(r), get<2>(r) = gcd(a, b)
*/
static tuple<i64, i64, i64> ext_gcd(i64 a, i64 b) {
auto ext_gcd_ = [](i64 a, i64 b, i64 &p, i64 &q, auto f) -> i64 {
if (b == 0) {
p = 1;
q = 0;
return a;
}
i64 d = f(b, a % b, q, p, f);
q -= a / b * p;
return d;
};
i64 p = 0, q = 0;
auto d = ext_gcd_(a, b, p, q, ext_gcd_);
return make_tuple(p, q, d);
}
static i64 lcm(i64 a, i64 b) {
auto x = gcd(a, b);
return a / x * b;
}
template <class Iterator>
static i64 lcm_ctr(const Iterator &begin, const Iterator &end) {
if (begin == end) {
return -1;
} else {
auto ans = *begin;
auto it = begin;
++it;
for (; it != end; ++it) {
auto x = *it;
ans = lcm(ans, x);
}
return ans;
}
}
static i64 lcm_ctr(const Vector<i64> &xs) {
return lcm_ctr(xs.begin(), xs.end());
}
template <class V> static V combination(V n, i64 r) {
if (r == 0) {
return 1;
}
V x = 1;
FOR(d, 1, r + 1) {
x *= n;
n -= 1;
x /= d;
}
return x;
}
static bool is_prime(i64 n) {
if (n <= 1)
return false;
for (i64 i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
static Vector<i64> divisor(i64 n) {
Vector<i64> res;
for (i64 i = 2; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
return res;
}
static HashMap<i64, size_t> prime_factor(i64 n) {
HashMap<i64, size_t> res;
for (i64 i = 2; i * i <= n; i++) {
while (n % i == 0) {
res[i] += 1;
n /= i;
}
}
if (n != 1)
res[n] = 1;
return res;
}
static pair<Vector<i64>, Vector<bool>> sieve(i64 n) {
Vector<i64> prime;
Vector<bool> is_prime_(n + 1, true);
is_prime_[0] = is_prime_[1] = false;
FOR(i, 2, n + 1) {
if (is_prime_[i]) {
prime.push_back(i);
for (i64 j = 2 * i; j <= n; j += i) {
is_prime_[j] = false;
}
}
}
return {prime, is_prime_};
}
/*
* x = b1 (mod m1)
* x = b2 (mod m2)
* => x = r.first (mod r.second) (r is a return value)
*/
static std::experimental::optional<pair<i64, i64>> chinese_rem(i64 b1, i64 m1,
i64 b2, i64 m2) {
auto elem = ext_gcd(m1, m2);
auto p = get<0>(elem);
auto d = get<2>(elem);
if ((b2 - b1) % d != 0)
return {};
i64 m = m1 * (m2 / d); //< lcm(m1, m2)
i64 r = mod(b1 + m1 * ((b2 - b1) / d * p % (m2 / d)), m);
return make_optional(std::make_pair(r, m));
}
template <typename Iterator1, typename Iterator2>
static std::experimental::optional<pair<i64, i64>>
chinese_rem_ctr(Iterator1 b_begin, Iterator1 b_end, Iterator2 m_begin,
Iterator2 m_end) {
i64 r = 0, M = 1;
auto b = b_begin;
auto m = m_begin;
for (; b != b_end && m != m_end; ++b, ++m) {
auto elem = ext_gcd(M, *m);
auto p = get<0>(elem);
auto d = get<2>(elem);
if ((*b - r) % d != 0)
return {};
r += M * ((*b - r) / d * p % (*m / d));
M *= *m / d;
}
return make_optional(std::make_pair(mod(r, M), M));
}
static std::experimental::optional<pair<i64, i64>>
chinese_rem_ctr(const Vector<i64> &b, const Vector<i64> &m) {
return chinese_rem_ctr<decltype(b.begin()), decltype(m.begin())>(CTR(b),
CTR(m));
}
void body() {
auto N = read<i64>();
auto M = read<i64>();
auto scs = read<i64, i64>(M);
auto begin = pow(10, N - 1);
if (N == 1) {
begin = 0;
}
auto end = pow(10, N);
FOR(c, begin, end) {
Vector<u8> digit;
auto c_ = c;
while (c_ != 0) {
digit.push_back(c_ % 10);
c_ /= 10;
}
bool f = true;
EACH(sc, scs) {
auto s = sc.first;
auto c = sc.second;
if (digit[digit.size() - s] != c) {
f = false;
break;
}
}
if (f) {
cout << c << endl;
return;
}
}
cout << -1 << endl;
}
| /*
URL https://
SCORE 0
AC false
WA false
TLE false
MLE false
TASK_TYPE
FAILURE_TYPE
NOTES
*/
#include <algorithm>
#include <cstdint>
#include <experimental/optional>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
/* import STL */
// stream
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::flush;
// basic types
using std::nullptr_t;
using std::pair;
using std::string;
using std::tuple;
using std::experimental::optional;
// function for basic types
using std::get;
using std::make_pair;
using std::make_tuple;
using std::experimental::make_optional;
/* TODO remove them */
using std::queue;
using std::stack;
using std::vector;
// algorithms
using std::accumulate;
using std::lower_bound;
using std::max_element;
using std::min_element;
using std::nth_element;
using std::upper_bound;
/* macros */
// loops
#define REP(i, n) for (i64 i = 0; i < static_cast<decltype(i)>(n); ++i)
#define REPR(i, n) for (i64 i = (n)-1; i >= static_cast<decltype(i)>(0); --i)
#define FOR(i, n, m) for (i64 i = (n); i < static_cast<decltype(i)>(m); ++i)
#define FORR(i, n, m) for (i64 i = (m)-1; i >= static_cast<decltype(i)>(n); --i)
#define EACH(x, xs) for (auto &x : (xs))
#define EACH_V(x, xs) for (auto x : (xs))
// helpers
#define CTR(x) (x).begin(), (x).end()
/* utils for std::tuple */
namespace internal {
namespace tuple_utils { // TODO rename to "internal::tuple"
template <size_t...> struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};
template <class Tuple, size_t... Is>
void read(std::istream &stream, Tuple &t, seq<Is...>) {
static_cast<void>((int[]){0, (void(stream >> get<Is>(t)), 0)...});
}
template <class Tuple, size_t... Is>
void print(std::ostream &stream, Tuple const &t, seq<Is...>) {
static_cast<void>(
(int[]){0, (void(stream << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...});
}
template <size_t I, class F, class A, class... Elems> struct ForEach {
void operator()(A &arg, tuple<Elems...> const &t) const {
F()(arg, get<I>(t));
ForEach<I - 1, F, A, Elems...>()(arg, t);
}
void operator()(A &arg, tuple<Elems...> &t) const {
F()(arg, get<I>(t));
ForEach<I - 1, F, A, Elems...>()(arg, t);
}
};
template <class F, class A, class... Elems> struct ForEach<0, F, A, Elems...> {
void operator()(A &arg, tuple<Elems...> const &t) const {
F()(arg, get<0>(t));
}
void operator()(A &arg, tuple<Elems...> &t) const { F()(arg, get<0>(t)); }
};
template <class F, class A, class... Elems>
void for_each(A &arg, tuple<Elems...> const &t) {
ForEach<std::tuple_size<tuple<Elems...>>::value - 1, F, A, Elems...>()(arg,
t);
}
template <class F, class A, class... Elems>
void for_each(A &arg, tuple<Elems...> &t) {
ForEach<std::tuple_size<tuple<Elems...>>::value - 1, F, A, Elems...>()(arg,
t);
}
} // namespace tuple_utils
} // namespace internal
/* utils for Matrix (definition of Matrix) */
namespace internal {
namespace matrix {
template <typename V, int N> struct matrix_t {
using type = std::vector<typename matrix_t<V, N - 1>::type>;
};
template <typename V> struct matrix_t<V, 0> {
using type = V;
};
template <typename V, int N> using Matrix = typename matrix_t<V, N>::type;
template <typename V, typename It, int N> struct matrix_helper {
static Matrix<V, N> create(const It &begin, const It &end,
const V &default_value) {
return Matrix<V, N>(*begin, matrix_helper<V, It, N - 1>::create(
begin + 1, end, default_value));
}
};
template <typename V, typename It> struct matrix_helper<V, It, 0> {
static Matrix<V, 0> create(const It &begin __attribute__((unused)),
const It &end __attribute__((unused)),
const V &default_value) {
return default_value;
}
};
} // namespace matrix
} // namespace internal
/* Primitive types */
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using usize = size_t;
/* Data structure type */
template <typename T> using Vector = std::vector<T>;
template <typename V> using OrderedSet = std::set<V>;
template <typename V> using HashSet = std::unordered_set<V>;
template <typename K, typename V> using OrderedMap = std::map<K, V>;
template <typename K, typename V> using HashMap = std::unordered_map<K, V>;
template <typename T, int N> using Matrix = internal::matrix::Matrix<T, N>;
template <typename T, typename Compare = std::less<T>,
typename Container = std::vector<T>>
using PriorityQueue = std::priority_queue<T, Container, Compare>;
/* utils for Vector */
template <typename V> Vector<V> make_pre_allocated_vector(size_t N) {
Vector<V> retval;
retval.reserve(N);
return retval;
}
/* utils for Matrix */
template <class V, int N>
Matrix<V, N> make_matrix(const std::array<size_t, N> &shape,
V default_value = V()) {
return internal::matrix::matrix_helper<V, decltype(shape.begin()), N>::create(
shape.begin(), shape.end(), default_value);
}
/* utils for STL iterators */
namespace internal {
template <typename Iterator, typename F> struct MappedIterator {
MappedIterator(const Iterator &it, const F &function)
: it(it), function(function) {}
auto operator*() const { return this->function(this->it); }
void operator++() { ++this->it; }
void operator+=(size_t n) { this->it += n; }
auto operator+(size_t n) const {
return MappedIterator<Iterator, F>(this->it + n, this->function);
}
bool operator==(const MappedIterator<Iterator, F> &rhs) const {
return this->it == rhs.it;
}
bool operator!=(const MappedIterator<Iterator, F> &rhs) const {
return !(*this == rhs);
}
private:
Iterator it;
F function;
};
template <typename Iterator, typename P> struct FilteredIterator {
FilteredIterator(const Iterator &it, const Iterator &end, const P &predicate)
: it(it), end(end), predicate(predicate) {
if (this->it != end) {
if (!predicate(this->it)) {
this->increment();
}
}
}
decltype(auto) operator*() const { return *this->it; }
auto operator->() const { return this->it; }
void operator++() { this->increment(); }
void operator+=(size_t n) {
REP(i, n) { this->increment(); }
}
auto operator+(size_t n) const {
auto retval = *this;
retval += n;
return retval;
}
bool operator==(const FilteredIterator<Iterator, P> &rhs) const {
return this->it == rhs.it;
}
bool operator!=(const FilteredIterator<Iterator, P> &rhs) const {
return !(*this == rhs);
}
private:
void increment() {
if (this->it == this->end) {
return;
}
++this->it;
while (this->it != this->end && !this->predicate(this->it)) {
++this->it;
}
}
Iterator it;
Iterator end;
P predicate;
};
template <typename Iterator, typename ElementIterator>
struct FlattenedIterator {
FlattenedIterator(const Iterator &it, const Iterator &end)
: it(make_pair(it, ElementIterator())), end(end) {
if (this->it.first != this->end) {
this->it.second = it->begin();
}
this->find_valid();
}
decltype(auto) operator*() const { return this->it; }
const pair<Iterator, ElementIterator> *operator->() const {
return &this->it;
}
void operator++() { this->increment(); }
void operator+=(size_t n) {
REP(i, n) { this->increment(); }
}
auto operator+(size_t n) const {
auto retval = *this;
retval += n;
return retval;
}
bool
operator==(const FlattenedIterator<Iterator, ElementIterator> &rhs) const {
if (this->it.first != rhs.it.first) {
return false;
}
if (this->it.first == this->end || rhs.it.first == rhs.end) {
if (this->end == rhs.end) {
return true;
} else {
return false;
}
} else {
return this->it.second == rhs.it.second;
}
}
bool
operator!=(const FlattenedIterator<Iterator, ElementIterator> &rhs) const {
return !(*this == rhs);
}
private:
bool is_end() const { return this->it.first == this->end; }
bool is_valid() const {
if (this->is_end())
return false;
if (this->it.second == this->it.first->end())
return false;
return true;
}
void _increment() {
if (this->it.second == this->it.first->end()) {
++this->it.first;
if (this->it.first != this->end) {
this->it.second = this->it.first->begin();
}
} else {
++this->it.second;
}
}
void find_valid() {
while (!this->is_end() && !this->is_valid()) {
this->_increment();
}
}
void increment() {
this->_increment();
while (!this->is_end() && !this->is_valid()) {
this->_increment();
}
}
pair<Iterator, ElementIterator> it;
Iterator end;
};
} // namespace internal
template <class Iterator> struct Container {
Container(const Iterator &begin, const Iterator &end)
: m_begin(begin), m_end(end) {}
const Iterator &begin() const { return this->m_begin; }
const Iterator &end() const { return this->m_end; }
Iterator m_begin;
Iterator m_end;
};
template <typename C, typename F> auto iterator_map(const C &c, F function) {
using Iterator =
std::remove_const_t<std::remove_reference_t<decltype(c.begin())>>;
return Container<internal::MappedIterator<Iterator, F>>(
internal::MappedIterator<Iterator, F>(c.begin(), function),
internal::MappedIterator<Iterator, F>(c.end(), function));
}
template <typename C, typename P>
auto iterator_filter(const C &c, P predicate) {
using Iterator =
std::remove_const_t<std::remove_reference_t<decltype(c.begin())>>;
return Container<internal::FilteredIterator<Iterator, P>>(
internal::FilteredIterator<Iterator, P>(c.begin(), c.end(), predicate),
internal::FilteredIterator<Iterator, P>(c.end(), c.end(), predicate));
}
template <typename C> auto iterator_flatten(const C &c) {
using Iterator =
std::remove_const_t<std::remove_reference_t<decltype(c.begin())>>;
using ElementIterator = std::remove_const_t<
std::remove_reference_t<decltype(c.begin()->begin())>>;
return Container<internal::FlattenedIterator<Iterator, ElementIterator>>(
internal::FlattenedIterator<Iterator, ElementIterator>(c.begin(),
c.end()),
internal::FlattenedIterator<Iterator, ElementIterator>(c.end(), c.end()));
}
/* input */
template <class F, class S>
std::istream &operator>>(std::istream &stream, pair<F, S> &pair) {
stream >> pair.first;
stream >> pair.second;
return stream;
}
template <class... Args>
std::istream &operator>>(std::istream &stream, tuple<Args...> &tuple) {
internal::tuple_utils::read(
stream, tuple, internal::tuple_utils::gen_seq<sizeof...(Args)>());
return stream;
}
template <class T> T read() {
T t;
cin >> t;
return t;
}
template <class F, class S> pair<F, S> read() {
pair<F, S> p;
cin >> p;
return p;
}
template <class T1, class T2, class T3, class... Args>
tuple<T1, T2, T3, Args...> read() {
tuple<T1, T2, T3, Args...> t;
cin >> t;
return t;
}
template <typename T, typename F = std::function<T()>>
Vector<T> read(const usize length, F r) {
auto retval = make_pre_allocated_vector<T>(length);
REP(i, length) { retval.emplace_back(r()); }
return retval;
}
template <class T> Vector<T> read(const usize length) {
return read<T>(length, [] { return read<T>(); });
}
template <class F, class S> Vector<pair<F, S>> read(const usize length) {
return read<pair<F, S>>(length);
}
template <class T1, class T2, class T3, class... Args>
Vector<tuple<T1, T2, T3, Args...>> read(const usize length) {
return read<tuple<T1, T2, T3, Args...>>(length);
}
namespace internal {
template <typename T> struct oneline {
std::string operator()(const T &t) const {
std::ostringstream oss;
oss << t;
return oss.str();
}
};
template <typename F, typename S> struct oneline<pair<F, S>> {
std::string operator()(const pair<F, S> &p) const {
std::ostringstream oss;
oss << "{" << oneline<F>()(p.first) << ", " << oneline<S>()(p.second)
<< "}";
return oss.str();
}
};
template <typename... Args> struct oneline<tuple<Args...>> {
struct oneline_tuple {
template <class V>
void operator()(Vector<std::string> &strs, const V &v) const {
strs.emplace_back(oneline<V>()(v));
}
};
std::string operator()(const tuple<Args...> &t) const {
std::ostringstream oss;
Vector<std::string> strs;
internal::tuple_utils::for_each<oneline_tuple, Vector<std::string>,
Args...>(strs, t);
oss << "{";
REPR(i, strs.size()) {
oss << strs[i];
if (i != 0) {
oss << ", ";
}
}
oss << "}";
return oss.str();
}
};
template <> struct oneline<bool> {
std::string operator()(const bool b) const { return b ? "true" : "false"; }
};
template <typename X> struct oneline<Vector<X>> {
std::string operator()(const Vector<X> &vec) const {
std::string retval = "[";
auto f = oneline<X>();
REP(i, vec.size()) {
retval += f(vec[i]);
if (i != static_cast<i64>(vec.size() - 1)) {
retval += ", ";
}
}
retval += "]";
return retval;
}
};
template <typename X> struct oneline<OrderedSet<X>> {
std::string operator()(const OrderedSet<X> &s) const {
std::string retval = "{";
auto f = oneline<X>();
size_t ctr = 0;
EACH(x, s) {
retval += f(x);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename X> struct oneline<HashSet<X>> {
std::string operator()(const HashSet<X> &s) const {
std::string retval = "{";
auto f = oneline<X>();
size_t ctr = 0;
EACH(x, s) {
retval += f(x);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename K, typename V> struct oneline<OrderedMap<K, V>> {
std::string operator()(const OrderedMap<K, V> &s) const {
std::string retval = "{";
auto f1 = oneline<K>();
auto f2 = oneline<V>();
size_t ctr = 0;
EACH(x, s) {
retval += f1(x.first);
retval += ": ";
retval += f2(x.second);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename K, typename V> struct oneline<HashMap<K, V>> {
std::string operator()(const HashMap<K, V> &s) const {
std::string retval = "{";
auto f1 = oneline<K>();
auto f2 = oneline<V>();
size_t ctr = 0;
EACH(x, s) {
retval += f1(x.first);
retval += ": ";
retval += f2(x.second);
ctr += 1;
if (ctr != s.size()) {
retval += ", ";
}
}
retval += "}";
return retval;
}
};
template <typename V> struct keys { /* no implementation */
};
template <typename X> struct keys<std::vector<X>> {
Vector<size_t> operator()(const Vector<X> &v) const {
Vector<size_t> keys;
REP(i, v.size()) { keys.emplace_back(i); }
return keys;
}
};
template <typename K, typename V> struct keys<OrderedMap<K, V>> {
Vector<K> operator()(const OrderedMap<K, V> &c) const {
Vector<K> keys;
EACH(elem, c) { keys.emplace_back(elem.first); }
return keys;
}
};
template <typename K, typename V> struct keys<HashMap<K, V>> {
Vector<K> operator()(const HashMap<K, V> &c) const {
Vector<K> keys;
EACH(elem, c) { keys.emplace_back(elem.first); }
return keys;
}
};
} // namespace internal
template <typename T> void dump(const T &t) {
using namespace internal;
std::cerr << oneline<T>()(t) << std::endl;
}
template <typename V1, typename V2, typename... Args>
void dump(const V1 &v1, const V2 &v2, const Args &...args) {
using namespace internal;
using F = typename oneline<tuple<V1, V2, Args...>>::oneline_tuple;
auto x = std::make_tuple(v1, v2, args...);
Vector<std::string> strs;
internal::tuple_utils::for_each<F, Vector<std::string>, V1, V2, Args...>(strs,
x);
REPR(i, strs.size()) {
std::cerr << strs[i];
if (i != 0) {
std::cerr << ", ";
}
}
std::cerr << std::endl;
}
template <typename C> std::string as_set(const C &ctr) {
Vector<std::string> values;
using namespace internal;
EACH(x, ctr) { values.emplace_back(oneline<decltype(x)>()(x)); }
std::string retval = "---\n";
REP(i, values.size()) {
retval += values[i];
retval += "\n";
}
retval += "---";
return retval;
}
template <typename C> std::string as_map(const C &ctr) {
using namespace internal;
auto ks = keys<C>()(ctr);
Vector<std::string> keys;
Vector<std::string> values;
EACH(key, ks) {
keys.emplace_back(oneline<decltype(key)>()(key));
values.emplace_back(oneline<decltype(ctr.at(key))>()(ctr.at(key)));
}
size_t l = 0;
EACH(key, keys) { l = std::max(l, key.size()); }
std::string retval = "---\n";
REP(i, values.size()) {
retval += keys[i];
REP(j, l - keys[i].size()) { retval += " "; }
retval += ": ";
retval += values[i];
retval += "\n";
}
retval += "---";
return retval;
}
template <typename C> std::string as_table(const C &ctr) {
using namespace internal;
auto rkeys = keys<C>()(ctr);
auto ckeys = OrderedSet<std::string>();
auto values =
Vector<pair<std::string, OrderedMap<std::string, std::string>>>();
/* Stringify all data */
EACH(rkey, rkeys) {
auto rkey_str = oneline<decltype(rkey)>()(rkey);
values.emplace_back(rkey_str, OrderedMap<std::string, std::string>());
auto row = ctr.at(rkey);
auto ks = keys<decltype(row)>()(row);
EACH(ckey, ks) {
auto ckey_str = oneline<decltype(ckey)>()(ckey);
ckeys.emplace(ckey_str);
values.back().second.emplace(
ckey_str, oneline<decltype(row.at(ckey))>()(row.at(ckey)));
}
}
/* Calculate string length */
size_t max_row_key_length = 0;
EACH(value, values) {
max_row_key_length = std::max(max_row_key_length, value.first.size());
}
OrderedMap<std::string, size_t> max_col_length;
EACH(ckey, ckeys) { max_col_length.emplace(ckey, ckey.size()); }
EACH(value, values) {
EACH(elem, value.second) {
auto ckey = elem.first;
auto value = elem.second;
max_col_length[ckey] = std::max(max_col_length[ckey], value.size());
}
}
std::string retval = "---\n";
/* Header */
REP(i, max_row_key_length) { retval += " "; }
retval += " ";
size_t cnt = 0;
EACH(ckey, ckeys) {
retval += ckey;
REP(j, max_col_length[ckey] - ckey.size()) { retval += " "; }
cnt += 1;
if (cnt != ckeys.size()) {
retval += ", ";
}
}
retval += "\n------\n";
/* Values */
EACH(value, values) {
retval += value.first;
REP(i, max_row_key_length - value.first.size()) { retval += " "; }
retval += "| ";
size_t cnt = 0;
EACH(ckey, ckeys) {
auto v = std::string("");
if (value.second.find(ckey) != value.second.end()) {
v = value.second.at(ckey);
}
retval += v;
REP(j, max_col_length[ckey] - v.size()) { retval += " "; }
cnt += 1;
if (cnt != ckeys.size()) {
retval += ", ";
}
}
retval += "\n";
}
retval += "---";
return retval;
}
// Hash
namespace std {
template <class F, class S> struct hash<pair<F, S>> {
size_t operator()(const pair<F, S> &p) const {
return hash<F>()(p.first) ^ hash<S>()(p.second);
}
};
template <class... Args> struct hash<tuple<Args...>> {
struct hash_for_element {
template <class V> void operator()(size_t &size, const V &v) const {
size ^= std::hash<V>()(v);
}
};
size_t operator()(const tuple<Args...> &t) const {
size_t retval = 0;
internal::tuple_utils::for_each<hash_for_element, size_t, Args...>(retval,
t);
return retval;
}
};
} // namespace std
#define MAIN
void body();
// main function (DO NOT EDIT)
int main(int argc, char **argv) {
cin.tie(0);
std::ios_base::sync_with_stdio(false);
cout << std::fixed;
body();
return 0;
}
#ifndef MAIN
#include "common.cc"
#endif
static i64 mod(i64 a, i64 m) { return (a % m + m) % m; }
/*
* n^r
*/
template <class V> static V pow(V n, i64 r) {
if (r == 0) {
return 1;
}
auto r2 = r / 2;
auto x2 = pow(n, r2);
return x2 * x2 * ((r % 2 == 0) ? 1 : n);
}
static i64 gcd(i64 a, i64 b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <class Iterator>
static i64 gcd_ctr(const Iterator &begin, const Iterator &end) {
if (begin == end) {
return -1;
} else {
auto ans = *begin;
auto it = begin;
++it;
for (; it != end; ++it) {
auto x = *it;
ans = gcd(ans, x);
}
return ans;
}
}
static i64 gcd_ctr(const Vector<i64> &xs) {
return gcd_ctr(xs.begin(), xs.end());
}
/*
* a * get<0>(r) + b * get<1>(r) = get<2>(r), get<2>(r) = gcd(a, b)
*/
static tuple<i64, i64, i64> ext_gcd(i64 a, i64 b) {
auto ext_gcd_ = [](i64 a, i64 b, i64 &p, i64 &q, auto f) -> i64 {
if (b == 0) {
p = 1;
q = 0;
return a;
}
i64 d = f(b, a % b, q, p, f);
q -= a / b * p;
return d;
};
i64 p = 0, q = 0;
auto d = ext_gcd_(a, b, p, q, ext_gcd_);
return make_tuple(p, q, d);
}
static i64 lcm(i64 a, i64 b) {
auto x = gcd(a, b);
return a / x * b;
}
template <class Iterator>
static i64 lcm_ctr(const Iterator &begin, const Iterator &end) {
if (begin == end) {
return -1;
} else {
auto ans = *begin;
auto it = begin;
++it;
for (; it != end; ++it) {
auto x = *it;
ans = lcm(ans, x);
}
return ans;
}
}
static i64 lcm_ctr(const Vector<i64> &xs) {
return lcm_ctr(xs.begin(), xs.end());
}
template <class V> static V combination(V n, i64 r) {
if (r == 0) {
return 1;
}
V x = 1;
FOR(d, 1, r + 1) {
x *= n;
n -= 1;
x /= d;
}
return x;
}
static bool is_prime(i64 n) {
if (n <= 1)
return false;
for (i64 i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
static Vector<i64> divisor(i64 n) {
Vector<i64> res;
for (i64 i = 2; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
return res;
}
static HashMap<i64, size_t> prime_factor(i64 n) {
HashMap<i64, size_t> res;
for (i64 i = 2; i * i <= n; i++) {
while (n % i == 0) {
res[i] += 1;
n /= i;
}
}
if (n != 1)
res[n] = 1;
return res;
}
static pair<Vector<i64>, Vector<bool>> sieve(i64 n) {
Vector<i64> prime;
Vector<bool> is_prime_(n + 1, true);
is_prime_[0] = is_prime_[1] = false;
FOR(i, 2, n + 1) {
if (is_prime_[i]) {
prime.push_back(i);
for (i64 j = 2 * i; j <= n; j += i) {
is_prime_[j] = false;
}
}
}
return {prime, is_prime_};
}
/*
* x = b1 (mod m1)
* x = b2 (mod m2)
* => x = r.first (mod r.second) (r is a return value)
*/
static std::experimental::optional<pair<i64, i64>> chinese_rem(i64 b1, i64 m1,
i64 b2, i64 m2) {
auto elem = ext_gcd(m1, m2);
auto p = get<0>(elem);
auto d = get<2>(elem);
if ((b2 - b1) % d != 0)
return {};
i64 m = m1 * (m2 / d); //< lcm(m1, m2)
i64 r = mod(b1 + m1 * ((b2 - b1) / d * p % (m2 / d)), m);
return make_optional(std::make_pair(r, m));
}
template <typename Iterator1, typename Iterator2>
static std::experimental::optional<pair<i64, i64>>
chinese_rem_ctr(Iterator1 b_begin, Iterator1 b_end, Iterator2 m_begin,
Iterator2 m_end) {
i64 r = 0, M = 1;
auto b = b_begin;
auto m = m_begin;
for (; b != b_end && m != m_end; ++b, ++m) {
auto elem = ext_gcd(M, *m);
auto p = get<0>(elem);
auto d = get<2>(elem);
if ((*b - r) % d != 0)
return {};
r += M * ((*b - r) / d * p % (*m / d));
M *= *m / d;
}
return make_optional(std::make_pair(mod(r, M), M));
}
static std::experimental::optional<pair<i64, i64>>
chinese_rem_ctr(const Vector<i64> &b, const Vector<i64> &m) {
return chinese_rem_ctr<decltype(b.begin()), decltype(m.begin())>(CTR(b),
CTR(m));
}
void body() {
auto N = read<i64>();
auto M = read<i64>();
auto scs = read<i64, i64>(M);
auto begin = pow(10, N - 1);
if (N == 1) {
begin = 0;
}
auto end = pow(10, N);
FOR(c, begin, end) {
Vector<u8> digit;
auto c_ = c;
if (c_ == 0) {
digit.push_back(0);
}
while (c_ != 0) {
digit.push_back(c_ % 10);
c_ /= 10;
}
bool f = true;
EACH(sc, scs) {
auto s = sc.first;
auto c = sc.second;
if (digit[digit.size() - s] != c) {
f = false;
break;
}
}
if (f) {
cout << c << endl;
return;
}
}
cout << -1 << endl;
}
| insert | 967 | 967 | 967 | 970 | 0 | |
p02761 | C++ | Runtime Error | // Ruthless Coding
#include <bits/stdc++.h>
#define uni(x) (x).resize(unique(all(x)) - (x).begin())
#define fprint(v) \
for (auto x : v) \
cout << x << ' '
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) make_pair(x, y)
#define SZ(x) int((x).size())
#define PB(x) push_back(x)
#define ll long long
#define S second
#define F first
#define nl '\n'
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
mt19937_64 rnd;
const int N = 2e5 + 5;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> cond;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
cond.PB(MP(x - 1, y));
}
for (int i = 0; i <= 999; i++) {
function<bool(int)> check = [&](int x) {
vector<int> res;
while (x > 0) {
res.PB(x % 10);
x /= 10;
}
reverse(ALL(res));
bool ok = SZ(res) == n;
for (auto &x : cond) {
ok &= (res[x.F] == x.S);
}
return ok;
};
if (check(i)) {
cout << i;
return 0;
}
}
cout << -1;
return 0;
}
/*
*** Most Impo.. -> check base case always
1. Overflow Check (*, +)
2. Index check (0 - based or 1 - based)
3. Check for n = 1, 2, 3, 4....
4. Corner Cases
*/ | // Ruthless Coding
#include <bits/stdc++.h>
#define uni(x) (x).resize(unique(all(x)) - (x).begin())
#define fprint(v) \
for (auto x : v) \
cout << x << ' '
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) make_pair(x, y)
#define SZ(x) int((x).size())
#define PB(x) push_back(x)
#define ll long long
#define S second
#define F first
#define nl '\n'
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
mt19937_64 rnd;
const int N = 2e5 + 5;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> cond;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
cond.PB(MP(x - 1, y));
}
for (int i = 0; i <= 999; i++) {
function<bool(int)> check = [&](int x) {
vector<int> res;
if (x == 0)
res.PB(0);
while (x > 0) {
res.PB(x % 10);
x /= 10;
}
reverse(ALL(res));
bool ok = SZ(res) == n;
for (auto &x : cond) {
ok &= (res[x.F] == x.S);
}
return ok;
};
if (check(i)) {
cout << i;
return 0;
}
}
cout << -1;
return 0;
}
/*
*** Most Impo.. -> check base case always
1. Overflow Check (*, +)
2. Index check (0 - based or 1 - based)
3. Check for n = 1, 2, 3, 4....
4. Corner Cases
*/ | insert | 77 | 77 | 77 | 79 | -11 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int INF = 1 << 29;
const ll MOD = 1000000007;
const int MAX = 200000;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
// 最大公約数
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
// 最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n, long long k) {
com[0] = 1;
for (int i = 1; i <= k; i++) {
com[i] = (com[i - 1] * (n - i + 1) % MOD) * modinv(i, MOD) % MOD;
}
}
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s[i] >> c[i];
}
bool ff = false;
if (N == 1) {
bool f = true;
for (int i = 1; i < M; i++) {
if (c[0] != c[i]) {
f = false;
}
}
if (f) {
cout << c[0] << endl;
ff = true;
}
} else if (N == 2) {
for (int i = 10; i < 100; i++) {
int x = i / 10;
int y = i % 10;
bool f = true;
for (int j = 0; j < M; j++) {
if (s[j] == 1) {
if (x != c[j])
f = false;
} else {
if (y != c[j])
f = false;
}
if (!f)
break;
}
if (f) {
cout << i << endl;
ff = true;
break;
}
}
} else {
for (int i = 100; i < 1000; i++) {
int x = i / 100;
int y = (i % 100) / 10;
int z = i % 10;
// cerr << i << "\t" << x << y << z << endl;
bool f = true;
for (int j = 0; j < M; j++) {
if (s[j] == 1) {
if (x != c[j])
f = false;
} else if (s[j] == 2) {
if (y != c[j])
f = false;
} else {
if (z != c[j])
f = false;
}
if (!f)
break;
}
if (f) {
cout << i << endl;
ff = true;
break;
}
}
}
if (!ff)
cout << -1 << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int INF = 1 << 29;
const ll MOD = 1000000007;
const int MAX = 200000;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
// 最大公約数
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
// 最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n, long long k) {
com[0] = 1;
for (int i = 1; i <= k; i++) {
com[i] = (com[i - 1] * (n - i + 1) % MOD) * modinv(i, MOD) % MOD;
}
}
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s[i] >> c[i];
}
bool ff = false;
if (M == 0) {
if (N == 1)
cout << 0 << endl;
if (N == 2)
cout << 10 << endl;
if (N == 3)
cout << 100 << endl;
return 0;
}
if (N == 1) {
bool f = true;
for (int i = 1; i < M; i++) {
if (c[0] != c[i]) {
f = false;
}
}
if (f) {
cout << c[0] << endl;
ff = true;
}
} else if (N == 2) {
for (int i = 10; i < 100; i++) {
int x = i / 10;
int y = i % 10;
bool f = true;
for (int j = 0; j < M; j++) {
if (s[j] == 1) {
if (x != c[j])
f = false;
} else {
if (y != c[j])
f = false;
}
if (!f)
break;
}
if (f) {
cout << i << endl;
ff = true;
break;
}
}
} else {
for (int i = 100; i < 1000; i++) {
int x = i / 100;
int y = (i % 100) / 10;
int z = i % 10;
// cerr << i << "\t" << x << y << z << endl;
bool f = true;
for (int j = 0; j < M; j++) {
if (s[j] == 1) {
if (x != c[j])
f = false;
} else if (s[j] == 2) {
if (y != c[j])
f = false;
} else {
if (z != c[j])
f = false;
}
if (!f)
break;
}
if (f) {
cout << i << endl;
ff = true;
break;
}
}
}
if (!ff)
cout << -1 << endl;
return 0;
}
| insert | 91 | 91 | 91 | 101 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }
int main() {
int n, m, ans = -1;
cin >> n >> m;
vector<pair<int, char>> sc(m);
rep(i, n) {
int s;
char c;
cin >> s >> c;
s--;
sc[i] = make_pair(s, c);
}
for (int i = 999; i >= 0; i--) {
str tmp = to_string(i);
if ((int)tmp.size() != n)
continue;
bool is_ok = true;
rep(j, m) if (tmp[sc[j].first] != sc[j].second) is_ok = false;
if (is_ok)
ans = i;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }
int main() {
int n, m, ans = -1;
cin >> n >> m;
vector<pair<int, char>> sc(m);
rep(i, m) {
int s;
char c;
cin >> s >> c;
s--;
sc[i] = make_pair(s, c);
}
for (int i = 999; i >= 0; i--) {
str tmp = to_string(i);
if ((int)tmp.size() != n)
continue;
bool is_ok = true;
rep(j, m) if (tmp[sc[j].first] != sc[j].second) is_ok = false;
if (is_ok)
ans = i;
}
cout << ans << endl;
return 0;
} | replace | 23 | 24 | 23 | 24 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> sc(M);
for (int i = 0; i < M; i++)
cin >> sc[i].first >> sc[i].second;
int init = 0;
int endon = 10;
if (N == 2) {
init = 10;
endon = 100;
}
if (N == 3) {
init = 100;
endon = 1000;
}
ll ans = 10000;
for (int i = init; i < endon; i++) {
for (int j = 0; j < M; j++) {
ll waru = 1;
if (sc[j].first == 1)
waru = 1;
if (sc[j].first == 2)
waru = 10;
if (sc[j].first == 3)
waru = 100;
if (i / (init / waru) % 10 != sc[j].second) {
break;
}
if (j == M - 1) {
if (i < ans)
ans = i;
}
}
}
if (ans != 10000)
cout << ans << endl;
else if (M == 0)
cout << init << endl;
else
cout << -1 << endl;
return 0;
}
// cout << setprecision(13);
// next_permutation();
//__gcd();
// reverse();
// set ,tuple ,pair;
// bitset
// vector.find
// vector.count
| #include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> sc(M);
for (int i = 0; i < M; i++)
cin >> sc[i].first >> sc[i].second;
int init = 0;
int endon = 10;
if (N == 2) {
init = 10;
endon = 100;
}
if (N == 3) {
init = 100;
endon = 1000;
}
ll ans = 10000;
for (int i = init; i < endon; i++) {
for (int j = 0; j < M; j++) {
ll waru = 1;
if (sc[j].first == 1)
waru = 1;
if (sc[j].first == 2)
waru = 10;
if (sc[j].first == 3)
waru = 100;
ll waruwaru = init / waru;
if (waruwaru == 0)
waruwaru = 1;
if ((i / waruwaru) % 10 != sc[j].second) {
break;
}
if (j == M - 1) {
if (i < ans)
ans = i;
}
}
}
if (ans != 10000)
cout << ans << endl;
else if (M == 0)
cout << init << endl;
else
cout << -1 << endl;
return 0;
}
// cout << setprecision(13);
// next_permutation();
//__gcd();
// reverse();
// set ,tuple ,pair;
// bitset
// vector.find
// vector.count
| replace | 51 | 52 | 51 | 55 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n, m;
cin >> n >> m;
vector<P> p(m);
rep(i, m) cin >> p[i].first >> p[i].second;
rep(x, 1000) {
int keta = 1;
int nx = x / 10;
vector<int> d(1, x % 10);
while (nx) {
keta++;
d.push_back(nx % 10);
nx / 10;
}
if (keta != n)
continue;
bool ok = true;
reverse(d.begin(), d.end());
rep(i, m) {
if (d[p[i].first - 1] != p[i].second)
ok = false;
}
if (ok) {
cout << x << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n, m;
cin >> n >> m;
vector<P> p(m);
rep(i, m) cin >> p[i].first >> p[i].second;
rep(x, 1000) {
int keta = 1;
int nx = x / 10;
vector<int> d(1, x % 10);
while (nx) {
keta++;
d.push_back(nx % 10);
nx /= 10;
}
if (keta != n)
continue;
bool ok = true;
reverse(d.begin(), d.end());
rep(i, m) {
if (d[p[i].first - 1] != p[i].second)
ok = false;
}
if (ok) {
cout << x << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} | replace | 18 | 19 | 18 | 19 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02761 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m), c(m);
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
}
// sが同じでcが違うようなものが存在すれば-1
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
// iとjを比べる
if (s[i] == s[j] && c[i] != c[j]) {
cout << -1 << endl;
return 0;
}
}
}
if (n == 1) {
cout << c[0] << endl;
return 0;
}
// 1桁目が0なら-1
for (int i = 0; i < m; i++) {
if (s[i] == 1 && c[i] == 0) {
cout << -1 << endl;
return 0;
}
}
vector<int> ans(n, 0);
ans[0] = 1;
for (int i = 0; i < m; i++) {
ans[s[i] - 1] = c[i];
}
for (int x : ans)
cout << x;
cout << endl;
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m), c(m);
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
}
// sが同じでcが違うようなものが存在すれば-1
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
// iとjを比べる
if (s[i] == s[j] && c[i] != c[j]) {
cout << -1 << endl;
return 0;
}
}
}
if (n == 1) {
cout << (m ? c[0] : 0) << endl;
return 0;
}
// 1桁目が0なら-1
for (int i = 0; i < m; i++) {
if (s[i] == 1 && c[i] == 0) {
cout << -1 << endl;
return 0;
}
}
vector<int> ans(n, 0);
ans[0] = 1;
for (int i = 0; i < m; i++) {
ans[s[i] - 1] = c[i];
}
for (int x : ans)
cout << x;
cout << endl;
return 0;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int N, M, s, c, Ans = 0;
cin >> N >> M;
vector<int> vec(3);
vec.at(0) = -1;
vec.at(1) = -1;
vec.at(2) = -1;
rep(i, M) {
cin >> s >> c;
if (vec.at(s - 1) == -1 || vec.at(s - 1) == c) {
vec.at(s - 1) = c;
} else {
Ans = -1;
break;
}
}
if (Ans == -1) {
cout << -1 << endl;
} else if (N == 1) {
if (vec.at(0) == -1) {
vec.at(0) = 0;
}
cout << vec.at(s - 1) << endl;
} else if (vec.at(0) == 0) {
cout << -1 << endl;
} else {
if (vec.at(0) == -1) {
vec.at(0) = 1;
}
if (vec.at(1) == -1) {
vec.at(1) = 0;
}
if (N == 2) {
cout << vec.at(0) * 10 + vec.at(1) << endl;
} else {
if (vec.at(2) == -1) {
vec.at(2) = 0;
}
cout << vec.at(0) * 100 + vec.at(1) * 10 + vec.at(2) << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int N, M, s, c, Ans = 0;
cin >> N >> M;
vector<int> vec(3);
vec.at(0) = -1;
vec.at(1) = -1;
vec.at(2) = -1;
rep(i, M) {
cin >> s >> c;
if (vec.at(s - 1) == -1 || vec.at(s - 1) == c) {
vec.at(s - 1) = c;
} else {
Ans = -1;
break;
}
}
if (Ans == -1) {
cout << -1 << endl;
} else if (N == 1) {
if (vec.at(0) == -1) {
vec.at(0) = 0;
}
cout << vec.at(0) << endl;
} else if (vec.at(0) == 0) {
cout << -1 << endl;
} else {
if (vec.at(0) == -1) {
vec.at(0) = 1;
}
if (vec.at(1) == -1) {
vec.at(1) = 0;
}
if (N == 2) {
cout << vec.at(0) * 10 + vec.at(1) << endl;
} else {
if (vec.at(2) == -1) {
vec.at(2) = 0;
}
cout << vec.at(0) * 100 + vec.at(1) * 10 + vec.at(2) << endl;
}
}
}
| replace | 27 | 28 | 27 | 28 | 0 | |
p02761 | C++ | Runtime Error | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<int> c(m);
for (int i = 0; i < m; i++)
cin >> s[i] >> c[i];
for (int i = 1; i < m; i++) {
for (int j = 0; j < i; j++) {
if (s[i] == s[j] && c[i] != c[j]) {
cout << -1 << endl;
return 0;
}
}
}
for (int i = 0; i < m; i++) {
if (n >= 2 && s[i] == 1 && c[i] == 0) {
cout << -1 << endl;
return 0;
}
}
if (n == 1) {
cout << c[0] << endl;
}
if (n == 2) {
int a = 1;
int b = 0;
for (int i = 0; i < m; i++) {
if (s[i] == 1)
a = c[i];
if (s[i] == 2)
b = c[i];
}
cout << 10 * a + b << endl;
}
if (n == 3) {
int p = 1;
int q = 0;
int r = 0;
for (int i = 0; i < m; i++) {
if (s[i] == 1)
p = c[i];
if (s[i] == 2)
q = c[i];
if (s[i] == 3)
r = c[i];
}
cout << 100 * p + 10 * q + r << endl;
}
} | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<int> c(m);
for (int i = 0; i < m; i++)
cin >> s[i] >> c[i];
if (m == 0) {
if (n == 1) {
cout << 0 << endl;
return 0;
}
if (n == 2) {
cout << 10 << endl;
return 0;
}
if (n == 3) {
cout << 100 << endl;
return 0;
}
}
for (int i = 1; i < m; i++) {
for (int j = 0; j < i; j++) {
if (s[i] == s[j] && c[i] != c[j]) {
cout << -1 << endl;
return 0;
}
}
}
for (int i = 0; i < m; i++) {
if (n >= 2 && s[i] == 1 && c[i] == 0) {
cout << -1 << endl;
return 0;
}
}
if (n == 1) {
cout << c[0] << endl;
}
if (n == 2) {
int a = 1;
int b = 0;
for (int i = 0; i < m; i++) {
if (s[i] == 1)
a = c[i];
if (s[i] == 2)
b = c[i];
}
cout << 10 * a + b << endl;
}
if (n == 3) {
int p = 1;
int q = 0;
int r = 0;
for (int i = 0; i < m; i++) {
if (s[i] == 1)
p = c[i];
if (s[i] == 2)
q = c[i];
if (s[i] == 3)
r = c[i];
}
cout << 100 * p + 10 * q + r << endl;
}
}
| insert | 14 | 14 | 14 | 28 | 0 | |
p02761 | C++ | Runtime Error | #include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const long INF = 1e9;
const long MOD = 1e9 + 7;
#define repi(i, n, init) for (int i = init; i < int(n); i++)
int power(int n, int t) {
int s = 1;
repi(i, t, 0) { s *= n; }
return s;
}
int main() {
int n, m, ans = -1;
cin >> n >> m;
vector<pair<int, int>> b(m);
repi(i, m, 0) {
int s, c;
cin >> s >> c;
b[i] = make_pair(s, c);
}
repi(i, power(10, n), power(10, n - 1) - 1) {
bool state = true;
repi(j, m, 0) {
string num = to_string(i);
if (num.at(b.at(j).first - 1) - '0' != b.at(j).second) {
state = false;
break;
}
}
if (state) {
cout << i << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | #include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const long INF = 1e9;
const long MOD = 1e9 + 7;
#define repi(i, n, init) for (int i = init; i < int(n); i++)
int power(int n, int t) {
int s = 1;
repi(i, t, 0) { s *= n; }
return s;
}
int main() {
int n, m, ans = -1;
cin >> n >> m;
vector<pair<int, int>> b(m);
repi(i, m, 0) {
int s, c;
cin >> s >> c;
b[i] = make_pair(s, c);
}
repi(i, power(10, n), power(10, n - 1) - (n == 1 ? 1 : 0)) {
bool state = true;
repi(j, m, 0) {
string num = to_string(i);
if (num.at(b.at(j).first - 1) - '0' != b.at(j).second) {
state = false;
break;
}
}
if (state) {
cout << i << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<int> ke(n, -1), c(m), s(m);
int flag = 1;
if (n == 1) {
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
}
for (int i = 0; i < m - 1; i++) {
if (c[i] == c[i + 1]) {
continue;
} else {
cout << -1 << endl;
return 0;
}
}
cout << c[0] << "\n";
return 0;
}
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
if (s[0] == 1 && c[0] == 0) {
flag = 0;
}
if (ke[s[i] - 1] == -1 || ke[s[i] - 1] == c[i]) {
ke[s[i] - 1] = c[i];
} else {
flag = 0;
}
}
if (ke[0] == -1) {
ke[0] = 1;
}
if (flag == 1) {
for (int i = 0; i < n; i++) {
if (ke[i] == -1) {
cout << 0;
} else {
cout << ke[i];
}
}
} else {
cout << -1;
}
cout << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<int> ke(n, -1), c(m), s(m);
int flag = 1;
if (n == 1 && m == 0) {
cout << 0 << endl;
return 0;
}
if (n == 1) {
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
}
for (int i = 0; i < m - 1; i++) {
if (c[i] == c[i + 1]) {
continue;
} else {
cout << -1 << endl;
return 0;
}
}
cout << c[0] << "\n";
return 0;
}
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
if (s[0] == 1 && c[0] == 0) {
flag = 0;
}
if (ke[s[i] - 1] == -1 || ke[s[i] - 1] == c[i]) {
ke[s[i] - 1] = c[i];
} else {
flag = 0;
}
}
if (ke[0] == -1) {
ke[0] = 1;
}
if (flag == 1) {
for (int i = 0; i < n; i++) {
if (ke[i] == -1) {
cout << 0;
} else {
cout << ke[i];
}
}
} else {
cout << -1;
}
cout << endl;
return 0;
} | insert | 18 | 18 | 18 | 23 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<int> c(m);
rep(i, m) { cin >> s.at(i) >> c.at(i); }
vector<int> t(m);
rep(i, n) t[i] = -1;
rep(j, m) {
if (t[s[j] - 1] == -1)
t[s[j] - 1] = c[j];
else if (t[s[j] - 1] != c[j]) {
cout << -1 << endl;
return 0;
}
}
if (n == 1)
t[0] = max(t[0], 0);
else if (t[0] != 0)
t[0] = max(t[0], 1);
else {
cout << -1 << endl;
return 0;
}
for (int i = 1; i < n; i++) {
if (t[i] == -1)
t[i] = 0;
}
int ans = 0;
int v;
for (int i = 0; i < n; i++) {
v = (int)pow(10, n - i - 1);
ans += t[i] * v;
// cout << t[i] << " " << v << endl;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<int> c(m);
rep(i, m) { cin >> s.at(i) >> c.at(i); }
vector<int> t(n);
rep(i, n) t[i] = -1;
rep(j, m) {
if (t[s[j] - 1] == -1)
t[s[j] - 1] = c[j];
else if (t[s[j] - 1] != c[j]) {
cout << -1 << endl;
return 0;
}
}
if (n == 1)
t[0] = max(t[0], 0);
else if (t[0] != 0)
t[0] = max(t[0], 1);
else {
cout << -1 << endl;
return 0;
}
for (int i = 1; i < n; i++) {
if (t[i] == -1)
t[i] = 0;
}
int ans = 0;
int v;
for (int i = 0; i < n; i++) {
v = (int)pow(10, n - i - 1);
ans += t[i] * v;
// cout << t[i] << " " << v << endl;
}
cout << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using vvii = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const ll inf = 1LL << 60;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
// cout << fixed << setprecision(15);
int N, M;
cin >> N >> M;
vector<int> S(M), C(M);
vector<int> exist(M, -1);
bool flag = true;
REP(i, M) {
int s, c;
cin >> s >> c;
if (exist[s - 1] == -1) {
exist[s - 1] = c;
} else {
if (exist[s - 1] == c)
continue;
else {
flag = false;
break;
}
}
}
if (!flag || (N > 1 && exist[0] == 0)) {
cout << -1 << endl;
return 0;
}
REP(i, N) {
if (exist[i] == -1) {
if (i == 0 && N > 1)
exist[i] = 1;
else
exist[i] = 0;
}
}
int ans = 0;
REP(i, N) { ans += exist[i] * pow(10, N - i - 1); }
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using vvii = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const ll inf = 1LL << 60;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
// cout << fixed << setprecision(15);
int N, M;
cin >> N >> M;
vector<int> S(M), C(M);
vector<int> exist(N, -1);
bool flag = true;
REP(i, M) {
int s, c;
cin >> s >> c;
if (exist[s - 1] == -1) {
exist[s - 1] = c;
} else {
if (exist[s - 1] == c)
continue;
else {
flag = false;
break;
}
}
}
if (!flag || (N > 1 && exist[0] == 0)) {
cout << -1 << endl;
return 0;
}
REP(i, N) {
if (exist[i] == -1) {
if (i == 0 && N > 1)
exist[i] = 1;
else
exist[i] = 0;
}
}
int ans = 0;
REP(i, N) { ans += exist[i] * pow(10, N - i - 1); }
cout << ans << endl;
} | replace | 28 | 29 | 28 | 29 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)
// lower_bound(A.begin(), A.end(), N)
// upper_bound(...
// A.erase(unique(A.begin(), A.end()), A.end())
// bit: &/and, |/or, ^/xor, ~/not
// getline(cin, String)
// while (getline(cin, S)) {}
// cout <<fixed <<setprecision(10)
// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)
//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 1,000,000 -> Loop
// 100,000 -> vector(int:400KB, long:800KB)
#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2
#define MOD 1000000007
#define Rep0(i, n) for (int i = 0; i < n; i++)
#define Rep1(i, n) for (int i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N, -1);
Rep0(i, M) {
int s, c;
cin >> s >> c;
s--;
if (c == A.at(s))
continue;
if (A.at(s) == -1)
A.at(s) = c;
else {
cout << -1 << endl;
return 0;
}
}
if (1 == N)
if (-1 == A.at(0)) {
cout << 0 << endl;
return 0;
}
if (1 != N)
if (0 == A.at(0)) {
cout << -1 << endl;
return 0;
}
if (-1 == A.at(0))
A.at(0) = 1;
for (int i = 1; i < N; i++)
if (-1 == A.at(i))
A.at(i) = 0;
Rep0(i, M) cout << A.at(i);
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)
// lower_bound(A.begin(), A.end(), N)
// upper_bound(...
// A.erase(unique(A.begin(), A.end()), A.end())
// bit: &/and, |/or, ^/xor, ~/not
// getline(cin, String)
// while (getline(cin, S)) {}
// cout <<fixed <<setprecision(10)
// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;
// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)
//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 1,000,000 -> Loop
// 100,000 -> vector(int:400KB, long:800KB)
#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2
#define MOD 1000000007
#define Rep0(i, n) for (int i = 0; i < n; i++)
#define Rep1(i, n) for (int i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N, -1);
Rep0(i, M) {
int s, c;
cin >> s >> c;
s--;
if (c == A.at(s))
continue;
if (A.at(s) == -1)
A.at(s) = c;
else {
cout << -1 << endl;
return 0;
}
}
if (1 == N)
if (-1 == A.at(0)) {
cout << 0 << endl;
return 0;
}
if (1 != N)
if (0 == A.at(0)) {
cout << -1 << endl;
return 0;
}
if (-1 == A.at(0))
A.at(0) = 1;
for (int i = 1; i < N; i++)
if (-1 == A.at(i))
A.at(i) = 0;
Rep0(i, N) cout << A.at(i);
cout << endl;
return 0;
}
| replace | 84 | 85 | 84 | 85 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int times10(int n) {
int output = 1;
for (int i = 0; i < n; i++) {
output *= 10;
}
return output;
}
int main() {
// enter
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s[i] >> c[i];
s[i]--;
}
vector<int> output(N, -1);
// program
if (M == 0) {
if (N == 1) {
cout << 0 << endl;
return 0;
} else if (N == 2) {
cout << 10 << endl;
return 0;
} else {
cout << 10 << endl;
return 100;
}
}
for (int i = 0; i < M; i++) {
if (N > 1 && s[i] == 0 && c[i] == 0) {
cout << -1 << endl;
return 0;
}
if (output[s[i]] == -1) {
output[s[i]] = c[i];
} else if (output[s[i]] == c[i]) {
continue;
} else {
cout << -1 << endl;
return 0;
}
}
int num;
if (output[0] == -1) {
if (N == 0) {
num = 0;
} else {
num = times10(N - 1);
}
} else {
num = times10(N - 1) * output[0];
}
for (int i = 1; i < N; i++) {
if (output[i] != -1) {
num += times10(N - 1 - i) * output[i];
}
}
cout << num << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int times10(int n) {
int output = 1;
for (int i = 0; i < n; i++) {
output *= 10;
}
return output;
}
int main() {
// enter
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s[i] >> c[i];
s[i]--;
}
vector<int> output(N, -1);
// program
if (M == 0) {
if (N == 1) {
cout << 0 << endl;
return 0;
} else if (N == 2) {
cout << 10 << endl;
return 0;
} else {
cout << 100 << endl;
return 0;
}
}
for (int i = 0; i < M; i++) {
if (N > 1 && s[i] == 0 && c[i] == 0) {
cout << -1 << endl;
return 0;
}
if (output[s[i]] == -1) {
output[s[i]] = c[i];
} else if (output[s[i]] == c[i]) {
continue;
} else {
cout << -1 << endl;
return 0;
}
}
int num;
if (output[0] == -1) {
if (N == 0) {
num = 0;
} else {
num = times10(N - 1);
}
} else {
num = times10(N - 1) * output[0];
}
for (int i = 1; i < N; i++) {
if (output[i] != -1) {
num += times10(N - 1 - i) * output[i];
}
}
cout << num << endl;
return 0;
}
| replace | 34 | 36 | 34 | 36 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define N 200100
int a[5];
void solve() {
int n, m;
cin >> n >> m;
memset(a, -1, sizeof a);
int sum = 0;
while (m--) {
int s, c;
cin >> s >> c;
if (a[s] == -1 || a[s] == c) {
sum += c;
a[s] = c;
} else {
cout << -1 << endl;
return;
}
}
if (n > 1 && a[1] == 0) {
cout << -1 << endl;
return;
}
for (int i = 1; i <= n; i++) {
if (i == 1 && a[i] == -1 && n > 1) {
cout << 1;
continue;
}
if (a[i] == -1)
cout << 0;
else
cout << a[i];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w+", stdout);
#endif
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define N 200100
int a[5];
void solve() {
int n, m;
cin >> n >> m;
memset(a, -1, sizeof a);
int sum = 0;
while (m--) {
int s, c;
cin >> s >> c;
if (a[s] == -1 || a[s] == c) {
sum += c;
a[s] = c;
} else {
cout << -1 << endl;
return;
}
}
if (n > 1 && a[1] == 0) {
cout << -1 << endl;
return;
}
for (int i = 1; i <= n; i++) {
if (i == 1 && a[i] == -1 && n > 1) {
cout << 1;
continue;
}
if (a[i] == -1)
cout << 0;
else
cout << a[i];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
} | delete | 41 | 45 | 41 | 41 | -11 | |
p02761 | Python | Runtime Error | n, m = map(int, input().split())
s, c = [], []
for i in range(m):
_s, _c = map(int, input().split())
s.append(_s)
c.append(_c)
for i in range(1000):
(*t,) = map(int, list(str(i)))
if len(t) != n:
continue
ok = True
for j in range(m):
if t[s[j]] != c[j]:
ok = False
break
if ok:
print(i)
quit()
print(-1)
| n, m = map(int, input().split())
s, c = [], []
for i in range(m):
_s, _c = map(int, input().split())
s.append(_s - 1)
c.append(_c)
for i in range(1000):
(*t,) = map(int, list(str(i)))
if len(t) != n:
continue
ok = True
for j in range(m):
if t[s[j]] != c[j]:
ok = False
break
if ok:
print(i)
quit()
print(-1)
| replace | 4 | 5 | 4 | 5 | IndexError: list index out of range | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02761/Python/s786393396.py", line 16, in <module>
if t[s[j]] != c[j]:
IndexError: list index out of range
|
p02761 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
int N, M, x = 100, y = 0, z = 0, ans;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
vector<int> d(3, 10);
rep(i, M) cin >> s[i] >> c[i];
if (N == 1) {
ans = c[0];
} else if (N == 2) {
rep(i, M) {
if (s[i] == 1) {
y = 10 * c[i];
} else {
z = c[i];
}
}
if (y == 0)
y = 10;
ans = y + z;
}
else if (N == 3) {
rep(i, M) {
if (s[i] == 1) {
x = 100 * c[i];
} else if (s[i] == 2) {
y = 10 * c[i];
} else {
z = c[i];
}
}
ans = x + y + z;
}
rep(i, M) {
if (d[s[i] - 1] == 10) {
d[s[i] - 1] = c[i];
} else {
if (d[s[i] - 1] != c[i])
ans = -1;
break;
}
}
rep(i, M) {
if (N == 2) {
if (s[i] == 1 && c[i] == 0)
ans = -1;
} else if (N == 3) {
if (s[i] == 1 && c[i] == 0)
ans = -1;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
int N, M, x = 100, y = 0, z = 0, ans;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
vector<int> d(3, 10);
rep(i, M) cin >> s[i] >> c[i];
if (N == 1) {
if (M != 0) {
ans = c[0];
} else {
ans = 0;
}
} else if (N == 2) {
rep(i, M) {
if (s[i] == 1) {
y = 10 * c[i];
} else {
z = c[i];
}
}
if (y == 0)
y = 10;
ans = y + z;
}
else if (N == 3) {
rep(i, M) {
if (s[i] == 1) {
x = 100 * c[i];
} else if (s[i] == 2) {
y = 10 * c[i];
} else {
z = c[i];
}
}
ans = x + y + z;
}
rep(i, M) {
if (d[s[i] - 1] == 10) {
d[s[i] - 1] = c[i];
} else {
if (d[s[i] - 1] != c[i])
ans = -1;
break;
}
}
rep(i, M) {
if (N == 2) {
if (s[i] == 1 && c[i] == 0)
ans = -1;
} else if (N == 3) {
if (s[i] == 1 && c[i] == 0)
ans = -1;
}
}
cout << ans << endl;
return 0;
} | replace | 18 | 19 | 18 | 24 | 0 | |
p02761 | C++ | Runtime Error |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ls(s) (s & (-s))
#define ll long long
#define inf 0x3f3f3f3f
const ll N = 500030;
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod = 1e9 + 7;
int main() {
int n, m;
cin >> n >> m;
vector<int> v(n);
rep(i, 0, n) v[i] = -1;
bool b = false;
vector<pair<int, int>> vp;
rep(i, 0, m) {
int x, y;
cin >> x >> y;
vp.push_back({x, y});
}
sort(vp.begin(), vp.end());
if (n == 1) {
if (m == 1) {
cout << vp[0].se;
return 0;
} else {
rep(i, 1, m) {
if (vp[i].se != vp[i - 1].se) {
cout << "-1";
return 0;
}
}
cout << vp[0].se;
return 0;
}
} else {
rep(i, 0, m) {
int x = vp[i].fi;
int y = vp[i].se;
if (x == 1) {
if (y <= 0)
b = true;
if (v[x - 1] == -1 || v[x - 1] == y)
v[x - 1] = y;
else
b = true;
} else {
if (v[x - 1] == -1 || v[x - 1] == y)
v[x - 1] = y;
else
b = true;
}
}
rep(i, 0, n) {
if (!i) {
if (v[i] == -1)
v[i] = 1;
if (v[i] <= 0)
b = true;
} else {
if (v[i] == -1)
v[i] = 0;
}
}
if (b)
cout << "-1";
else {
rep(i, 0, n) cout << v[i];
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ls(s) (s & (-s))
#define ll long long
#define inf 0x3f3f3f3f
const ll N = 500030;
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const ll mod = 1e9 + 7;
int main() {
int n, m;
cin >> n >> m;
vector<int> v(n);
rep(i, 0, n) v[i] = -1;
bool b = false;
vector<pair<int, int>> vp;
rep(i, 0, m) {
int x, y;
cin >> x >> y;
vp.push_back({x, y});
}
sort(vp.begin(), vp.end());
if (n == 1) {
if (!m) {
cout << 0;
return 0;
} else if (m == 1) {
cout << vp[0].se;
return 0;
} else {
rep(i, 1, m) {
if (vp[i].se != vp[i - 1].se) {
cout << "-1";
return 0;
}
}
cout << vp[0].se;
return 0;
}
} else {
rep(i, 0, m) {
int x = vp[i].fi;
int y = vp[i].se;
if (x == 1) {
if (y <= 0)
b = true;
if (v[x - 1] == -1 || v[x - 1] == y)
v[x - 1] = y;
else
b = true;
} else {
if (v[x - 1] == -1 || v[x - 1] == y)
v[x - 1] = y;
else
b = true;
}
}
rep(i, 0, n) {
if (!i) {
if (v[i] == -1)
v[i] = 1;
if (v[i] <= 0)
b = true;
} else {
if (v[i] == -1)
v[i] = 0;
}
}
if (b)
cout << "-1";
else {
rep(i, 0, n) cout << v[i];
}
}
} | replace | 30 | 31 | 30 | 34 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> kazu(n);
int f = 0;
int g = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
if (a == 1) {
g = 1;
}
if (kazu.at(a - 1) != 0 && kazu.at(a - 1) != b) {
f = -1;
} else {
kazu.at(a - 1) = b;
}
}
if (g == 0) {
kazu.at(1) == 1;
}
if (f != 0 || (n > 1 && kazu.at(0) == 0)) {
cout << -1 << endl;
} else {
for (int i = 0; i < n; i++) {
cout << kazu.at(i);
}
cout << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> kazu(n);
int f = 0;
int g = 0;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
if (a == 1) {
g = 1;
}
if (kazu.at(a - 1) != 0 && kazu.at(a - 1) != b) {
f = -1;
} else {
kazu.at(a - 1) = b;
}
}
if (g == 0 && n != 1) {
kazu.at(0) = 1;
}
if (f != 0 || (n > 1 && kazu.at(0) == 0)) {
cout << -1 << endl;
} else {
for (int i = 0; i < n; i++) {
cout << kazu.at(i);
}
cout << endl;
}
}
| replace | 21 | 23 | 21 | 23 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> a;
rep(i, m) { cin >> a[i].first >> a[i].second; }
rep(x, 1000) {
int keta = 1;
vector<int> d(1, x % 10);
int xx = x / 10;
while (xx) {
keta++;
d.push_back(xx % 10);
xx /= 10;
}
reverse(d.begin(), d.end());
if (keta == n) {
bool ok = true;
rep(i, m) if (d[a[i].first - 1] != a[i].second) ok = false;
if (ok) {
cout << x << endl;
return 0;
}
} else
continue;
}
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(m);
rep(i, m) { cin >> a[i].first >> a[i].second; }
rep(x, 1000) {
int keta = 1;
vector<int> d(1, x % 10);
int xx = x / 10;
while (xx) {
keta++;
d.push_back(xx % 10);
xx /= 10;
}
reverse(d.begin(), d.end());
if (keta == n) {
bool ok = true;
rep(i, m) if (d[a[i].first - 1] != a[i].second) ok = false;
if (ok) {
cout << x << endl;
return 0;
}
} else
continue;
}
cout << -1 << endl;
return 0;
}
| replace | 8 | 9 | 8 | 9 | -11 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ll n, m;
cin >> n >> m;
ll s[m], c[m];
for (int i = 1; i <= m; ++i) {
cin >> s[i] >> c[i];
}
map<ll, ll> mp;
for (int i = 1; i <= n; ++i) {
mp[i] = -1;
}
for (int i = 1; i <= m; ++i) {
if (c[i] == 0 && s[i] == 1 && n != 1) {
cout << -1;
return 0;
}
if (mp[s[i]] != c[i] && mp[s[i]] != -1) {
// cout<<c[i];
cout << -1;
return 0;
}
mp[s[i]] = c[i];
}
for (int i = 1; i <= n; ++i) {
if (i == 1 && mp[i] == -1 && n != 1) {
mp[i] = 1;
} else if (mp[i] == -1) {
mp[i] = 0;
}
}
for (int i = 1; i <= n; ++i) {
cout << mp[i];
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, m;
cin >> n >> m;
ll s[m], c[m];
for (int i = 1; i <= m; ++i) {
cin >> s[i] >> c[i];
}
map<ll, ll> mp;
for (int i = 1; i <= n; ++i) {
mp[i] = -1;
}
for (int i = 1; i <= m; ++i) {
if (c[i] == 0 && s[i] == 1 && n != 1) {
cout << -1;
return 0;
}
if (mp[s[i]] != c[i] && mp[s[i]] != -1) {
// cout<<c[i];
cout << -1;
return 0;
}
mp[s[i]] = c[i];
}
for (int i = 1; i <= n; ++i) {
if (i == 1 && mp[i] == -1 && n != 1) {
mp[i] = 1;
} else if (mp[i] == -1) {
mp[i] = 0;
}
}
for (int i = 1; i <= n; ++i) {
cout << mp[i];
}
}
| replace | 5 | 11 | 5 | 6 | -11 | |
p02761 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main(void) {
IOS int n, m;
cin >> n >> m;
vector<pii> v;
REP(i, m) {
int s, c;
cin >> s >> c;
s--;
v.emplace_back(s, c);
}
int i = n == 1 ? 0 : pow(10, n - 1);
int e = pow(10, n);
while (i < e) {
string s = to_string(i);
bool ok = true;
REP(j, m) {
if (s[v[j].first] != v[j].second + '0')
ok = false;
}
if (ok) {
cout << s << '\n';
return 0;
}
}
OUT(-1)
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main(void) {
IOS int n, m;
cin >> n >> m;
vector<pii> v;
REP(i, m) {
int s, c;
cin >> s >> c;
s--;
v.emplace_back(s, c);
}
int i = n == 1 ? 0 : pow(10, n - 1);
int e = pow(10, n);
while (i < e) {
string s = to_string(i);
bool ok = true;
REP(j, m) {
if (s[v[j].first] != v[j].second + '0')
ok = false;
}
if (ok) {
cout << s << '\n';
return 0;
}
i++;
}
OUT(-1)
return 0;
} | insert | 42 | 42 | 42 | 43 | TLE | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char> CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define llower(v, hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v, hoge) *upper_bound(v.begin(), v.end(), hoge)
#define toupper(S) transform(S.begin(), S.end(), S.begin(), toupper)
#define tolower(S) transform(S.begin(), S.end(), S.begin(), tolower)
// const lint MOD = pow(10, 9) + 7;
int main() {
int N, M;
cin >> N >> M;
vector<IP> SC(M);
rep(i, M) { cin >> SC[i].first >> SC[i].second; }
sort(SC);
/*rep(i, M) {
cout << SC[i].first << ' ' << SC[i].second << endl;
}*/
vector<int> ans(N, 0);
if (M >= 2) {
rep(i, M - 1) {
if (SC[i].first == SC[i + 1].first && SC[i].second != SC[i + 1].second) {
cout << -1 << endl;
return 0;
}
}
}
if (N != 1) {
if (SC[0].first == 1 && SC[0].second == 0) {
cout << -1 << endl;
return 0;
}
}
rep(i, M) { ans[SC[i].first - 1] = SC[i].second; }
if (N == 1) {
cout << ans[0] << endl;
}
else {
rep(i, N) {
if (i == 0) {
if (ans[i] == 0) {
cout << 1;
} else {
cout << ans[i];
}
} else {
cout << ans[i];
}
}
cout << endl;
}
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int lint;
typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char> CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v, hoge) upper_bound(v.begin(), v.end(), hoge)
#define lower(v, hoge) lower_bound(v.begin(), v.end(), hoge)
#define llower(v, hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v, hoge) *upper_bound(v.begin(), v.end(), hoge)
#define toupper(S) transform(S.begin(), S.end(), S.begin(), toupper)
#define tolower(S) transform(S.begin(), S.end(), S.begin(), tolower)
// const lint MOD = pow(10, 9) + 7;
int main() {
int N, M;
cin >> N >> M;
if (M == 0) {
if (N == 1) {
cout << 0 << endl;
return 0;
} else if (N == 2) {
cout << 10 << endl;
return 0;
} else {
cout << 100 << endl;
return 0;
}
}
vector<IP> SC(M);
rep(i, M) { cin >> SC[i].first >> SC[i].second; }
sort(SC);
/*rep(i, M) {
cout << SC[i].first << ' ' << SC[i].second << endl;
}*/
vector<int> ans(N, 0);
if (M >= 2) {
rep(i, M - 1) {
if (SC[i].first == SC[i + 1].first && SC[i].second != SC[i + 1].second) {
cout << -1 << endl;
return 0;
}
}
}
if (N != 1) {
if (SC[0].first == 1 && SC[0].second == 0) {
cout << -1 << endl;
return 0;
}
}
rep(i, M) { ans[SC[i].first - 1] = SC[i].second; }
if (N == 1) {
cout << ans[0] << endl;
}
else {
rep(i, N) {
if (i == 0) {
if (ans[i] == 0) {
cout << 1;
} else {
cout << ans[i];
}
} else {
cout << ans[i];
}
}
cout << endl;
}
}
| insert | 40 | 40 | 40 | 52 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<char> c(M);
for (int i = 0; i < M; i++)
cin >> s.at(i) >> c.at(i);
if (N == 1) {
bool flag = true;
for (int i = 0; i < M - 1; i++)
if (c.at(i) != c.at(i + 1))
flag = false;
if (flag)
cout << c.at(0) << endl;
else
cout << -1 << endl;
return 0;
}
for (int i = pow(10, N - 1); i < pow(10, N); i++) {
string tmp = to_string(i);
bool flag = true;
for (int j = 0; j < M; j++) {
if (tmp.at(s.at(j) - 1) != c.at(j))
flag = false;
}
if (flag) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<char> c(M);
for (int i = 0; i < M; i++)
cin >> s.at(i) >> c.at(i);
if (M == 0) {
if (N == 1)
cout << 0 << endl;
else
cout << pow(10, N - 1) << endl;
return 0;
}
if (N == 1) {
bool flag = true;
for (int i = 0; i < M - 1; i++)
if (c.at(i) != c.at(i + 1))
flag = false;
if (flag)
cout << c.at(0) << endl;
else
cout << -1 << endl;
return 0;
}
for (int i = pow(10, N - 1); i < pow(10, N); i++) {
string tmp = to_string(i);
bool flag = true;
for (int j = 0; j < M; j++) {
if (tmp.at(s.at(j) - 1) != c.at(j))
flag = false;
}
if (flag) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| insert | 10 | 10 | 10 | 18 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> p(n + 1, vector<int>(0));
rep(i, m) {
int x, y;
cin >> x >> y;
p[x].push_back(y);
}
// is duplicated?
rep1(i, n) {
if (p[i].size() < 2)
continue;
rep(j, p[i].size() - 1) {
if (p[i][j] != p[i][j + 1]) {
cout << -1 << endl;
return 0;
}
}
}
// is Top 0?
if (p[1][0] == 0) {
cout << -1 << endl;
return 0;
}
// ans
rep1(i, n) {
if (p[i].size() > 0)
cout << p[i][0];
else
cout << 0;
}
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> p(n + 1, vector<int>(0));
rep(i, m) {
int x, y;
cin >> x >> y;
p[x].push_back(y);
}
// is duplicated?
rep1(i, n) {
if (p[i].size() < 2)
continue;
rep(j, p[i].size() - 1) {
if (p[i][j] != p[i][j + 1]) {
cout << -1 << endl;
return 0;
}
}
}
// is Top 0?
if (p[1].size() > 0) {
if (p[1][0] == 0) {
if (n > 1) {
cout << -1 << endl;
return 0;
}
}
} else {
if (n > 1) {
p[1].push_back(1);
} else {
p[1].push_back(0);
}
}
// ans
rep1(i, n) {
if (p[i].size() > 0)
cout << p[i][0];
else
cout << 0;
}
cout << endl;
}
| replace | 30 | 33 | 30 | 43 | 0 | |
p02761 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, char>> P(M);
int i;
if (N == 1) {
i = 0;
} else if (N == 2) {
i = 10;
} else {
i = 100;
}
for (int j = 0; j < M; j++) {
cin >> P.at(j).first >> P.at(j).second;
}
// cout<<1<<P.at(0).second;
while (true) {
string s;
s = to_string(i);
for (int j = 0; j < M; j++) {
if (s.at(P.at(j).first - 1) != P.at(j).second) {
goto PLAS;
}
}
cout << i;
break;
PLAS:
i++;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, char>> P(M);
int i;
if (N == 1) {
i = 0;
} else if (N == 2) {
i = 10;
} else {
i = 100;
}
for (int j = 0; j < M; j++) {
cin >> P.at(j).first >> P.at(j).second;
}
// cout<<1<<P.at(0).second;
while (true) {
string s;
s = to_string(i);
for (int j = 0; j < M; j++) {
if (s.at(P.at(j).first - 1) != P.at(j).second) {
goto PLAS;
}
}
cout << i;
break;
PLAS:
i++;
if (i == 1000) {
cout << -1;
break;
}
}
}
| insert | 31 | 31 | 31 | 35 | TLE | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int fuka = 0;
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s.at(i) >> c.at(i);
}
// 1keta no toki
if (N == 1) {
sort(c.begin(), c.end());
if (c.at(0) != c.at(M - 1)) {
fuka = 1;
}
if (fuka == 1) {
cout << -1 << endl;
} else {
cout << c.at(0) << endl;
}
} else {
// 2,3 keta no toki
for (int i = 0; i < M; i++) {
if (s.at(i) == 1 && c.at(i) == 0) {
fuka = 1;
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (s.at(i) == s.at(j) && c.at(i) != c.at(j)) {
fuka = 1;
}
}
}
// kokomade reigai shori
int r1, r2, r3;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
if (N == 3) {
for (int i = 0; i < M; i++) {
if (s.at(i) == 1) {
r1 = c.at(i);
flag1 = 1;
}
}
if (flag1 == 0) {
r1 = 1;
}
for (int i = 0; i < M; i++) {
if (s.at(i) == 2) {
r2 = c.at(i);
flag2 = 1;
}
}
if (flag2 == 0) {
r2 = 0;
}
for (int i = 0; i < M; i++) {
if (s.at(i) == 3) {
r3 = c.at(i);
flag3 = 1;
}
}
if (flag3 == 0) {
r3 = 0;
}
if (fuka == 0) {
cout << r1 * 100 + r2 * 10 + r3 << endl;
} else {
cout << -1 << endl;
}
// N=3 no kakko
}
if (N == 2) {
for (int i = 0; i < M; i++) {
if (s.at(i) == 1) {
r1 = c.at(i);
flag1 = 1;
}
}
if (flag1 == 0) {
r1 = 1;
}
for (int i = 0; i < M; i++) {
if (s.at(i) == 2) {
r2 = c.at(i);
flag2 = 1;
}
}
if (flag2 == 0) {
r2 = 0;
}
if (fuka == 0) {
cout << r1 * 10 + r2 << endl;
} else {
cout << -1 << endl;
}
}
// N=2,3 no kakko
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int fuka = 0;
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s.at(i) >> c.at(i);
}
// 1keta no toki
if (N == 1 && M == 0) {
cout << 0 << endl;
}
if (N == 1 && M > 0) {
sort(c.begin(), c.end());
if (c.at(0) != c.at(M - 1)) {
fuka = 1;
}
if (fuka == 1) {
cout << -1 << endl;
} else {
cout << c.at(0) << endl;
}
} else {
// 2,3 keta no toki
for (int i = 0; i < M; i++) {
if (s.at(i) == 1 && c.at(i) == 0) {
fuka = 1;
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (s.at(i) == s.at(j) && c.at(i) != c.at(j)) {
fuka = 1;
}
}
}
// kokomade reigai shori
int r1, r2, r3;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
if (N == 3) {
for (int i = 0; i < M; i++) {
if (s.at(i) == 1) {
r1 = c.at(i);
flag1 = 1;
}
}
if (flag1 == 0) {
r1 = 1;
}
for (int i = 0; i < M; i++) {
if (s.at(i) == 2) {
r2 = c.at(i);
flag2 = 1;
}
}
if (flag2 == 0) {
r2 = 0;
}
for (int i = 0; i < M; i++) {
if (s.at(i) == 3) {
r3 = c.at(i);
flag3 = 1;
}
}
if (flag3 == 0) {
r3 = 0;
}
if (fuka == 0) {
cout << r1 * 100 + r2 * 10 + r3 << endl;
} else {
cout << -1 << endl;
}
// N=3 no kakko
}
if (N == 2) {
for (int i = 0; i < M; i++) {
if (s.at(i) == 1) {
r1 = c.at(i);
flag1 = 1;
}
}
if (flag1 == 0) {
r1 = 1;
}
for (int i = 0; i < M; i++) {
if (s.at(i) == 2) {
r2 = c.at(i);
flag2 = 1;
}
}
if (flag2 == 0) {
r2 = 0;
}
if (fuka == 0) {
cout << r1 * 10 + r2 << endl;
} else {
cout << -1 << endl;
}
}
// N=2,3 no kakko
}
} | replace | 14 | 16 | 14 | 18 | 0 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<char> c(m);
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
s[i]--;
}
int upper, lower;
switch (n) {
case 1:
lower = 0;
upper = 10;
break;
case 2:
lower = 10;
upper = 100;
break;
case 3:
lower = 100;
upper = 1000;
break;
}
int ans = -1;
for (int i = lower; i < upper; i++) {
bool ok = true;
string t = to_string(i);
for (int j = 0; j < n; j++) {
if (t[s[j]] != c[j]) {
ok = false;
break;
}
}
if (ok) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<char> c(m);
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
s[i]--;
}
int upper, lower;
switch (n) {
case 1:
lower = 0;
upper = 10;
break;
case 2:
lower = 10;
upper = 100;
break;
case 3:
lower = 100;
upper = 1000;
break;
}
int ans = -1;
for (int i = lower; i < upper; i++) {
bool ok = true;
string t = to_string(i);
for (int j = 0; j < m; j++) {
if (t[s[j]] != c[j]) {
ok = false;
break;
}
}
if (ok) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
}
| replace | 41 | 42 | 41 | 42 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int j;
int M;
cin >> M;
vector<int> ans(N);
long answer;
answer = 0;
int i;
long jousuu;
vector<int> s(M);
vector<int> c(M);
for (i = 0; i < M; i++) {
cin >> s.at(i);
// cout << s.at(i);
cin >> c.at(i);
// cout << c.at(i);
}
ans.at(0) = 1;
int temp;
for (i = 0; i < M; i++) {
for (j = i + 1; j < M; j++) {
if (c.at(i) < c.at(j)) {
temp = c.at(i);
c.at(i) = c.at(j);
c.at(j) = temp;
temp = s.at(i);
s.at(i) = s.at(j);
s.at(j) = temp;
}
}
}
for (i = 0; i < M; i++) {
ans.at(s.at(i) - 1) = c.at(i);
// cout << ans.at(s.at(i)-1) << c.at(i) << endl;
}
jousuu = 1;
for (i = N - 1; i > 0; i--) {
jousuu = jousuu * 10;
}
for (i = 0; i < N; i++) {
answer = answer + ans.at(i) * jousuu;
jousuu = jousuu / 10;
}
// かぶりがあったらマイナス1
for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) {
if (i == j) {
continue;
}
if (s.at(i) == s.at(j)) {
if (c.at(i) != c.at(j)) {
answer = (-1);
}
}
}
}
for (i = 0; i < M; i++) {
if (s.at(i) == 1 && c.at(i) == 0) {
answer = (-1);
}
}
if (N == 1 && c.at(0) == 0) {
answer = 0;
}
/*
for(i=0;i<M-1;i++){
if(s.at(i)==s.at(i+1)){
if(c.at(i)!=c.at(i+1)){
cout << answer<< endl;
cout << s.at(i) << endl;
cout << s.at(i+1) << endl;
cout << c.at(i) << endl;
cout << c.at(i+1) << endl;
answer = (-1);
}
}
}
*/
cout << answer << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int j;
int M;
cin >> M;
vector<int> ans(N);
long answer;
answer = 0;
int i;
long jousuu;
vector<int> s(M);
vector<int> c(M);
for (i = 0; i < M; i++) {
cin >> s.at(i);
// cout << s.at(i);
cin >> c.at(i);
// cout << c.at(i);
}
ans.at(0) = 1;
int temp;
for (i = 0; i < M; i++) {
for (j = i + 1; j < M; j++) {
if (c.at(i) < c.at(j)) {
temp = c.at(i);
c.at(i) = c.at(j);
c.at(j) = temp;
temp = s.at(i);
s.at(i) = s.at(j);
s.at(j) = temp;
}
}
}
for (i = 0; i < M; i++) {
ans.at(s.at(i) - 1) = c.at(i);
// cout << ans.at(s.at(i)-1) << c.at(i) << endl;
}
jousuu = 1;
for (i = N - 1; i > 0; i--) {
jousuu = jousuu * 10;
}
for (i = 0; i < N; i++) {
answer = answer + ans.at(i) * jousuu;
jousuu = jousuu / 10;
}
// かぶりがあったらマイナス1
for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) {
if (i == j) {
continue;
}
if (s.at(i) == s.at(j)) {
if (c.at(i) != c.at(j)) {
answer = (-1);
}
}
}
}
for (i = 0; i < M; i++) {
if (s.at(i) == 1 && c.at(i) == 0) {
answer = (-1);
}
}
if (M != 0) {
if (N == 1 && s.at(0) == 1 && c.at(0) == 0) {
answer = 0;
}
} else {
if (N == 1) {
answer = 0;
}
}
/*
for(i=0;i<M-1;i++){
if(s.at(i)==s.at(i+1)){
if(c.at(i)!=c.at(i+1)){
cout << answer<< endl;
cout << s.at(i) << endl;
cout << s.at(i+1) << endl;
cout << c.at(i) << endl;
cout << c.at(i+1) << endl;
answer = (-1);
}
}
}
*/
cout << answer << endl;
} | replace | 70 | 73 | 70 | 78 | 0 | |
p02761 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<char> c(m);
rep(i, m) {
cin >> s[i] >> c[i];
s[i]--;
}
int ans = -1;
for (int i = pow(10, n - 1); i < pow(10, n); i++) {
if (n == 1 && i == 1)
i--;
string num = to_string(i);
bool f = true;
rep(j, m) {
if (num[s[j]] != c[j])
f = false;
}
if (f) {
ans = i;
break;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m);
vector<char> c(m);
rep(i, m) {
cin >> s[i] >> c[i];
s[i]--;
}
int ans = -1;
int st = pow(10, n - 1);
if (n == 1)
st = 0;
for (int i = st; i < pow(10, n); i++) {
string num = to_string(i);
bool f = true;
rep(j, m) {
if (num[s[j]] != c[j])
f = false;
}
if (f) {
ans = i;
break;
}
}
cout << ans << endl;
} | replace | 15 | 18 | 15 | 19 | TLE | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M), c(M);
for (int i = 0; i < M; i++)
cin >> s[i] >> c[i];
if (N == 1) {
for (int i = 1; i < M; i++) {
if (c[i] != c[0]) {
cout << -1 << endl;
return 0;
}
}
cout << c[0] << endl;
return 0;
}
vector<int> ans(N, -1);
for (int i = 0; i < M; i++) {
if (ans[s[i] - 1] == -1)
ans[s[i] - 1] = c[i];
else {
if (ans[s[i] - 1] != c[i]) {
cout << -1 << endl;
return 0;
}
}
}
for (int i = 0; i < N; i++) {
if (ans[i] == -1) {
if (i == 0)
ans[i] = 1;
else
ans[i] = 0;
}
if (ans[0] == 0) {
cout << -1 << endl;
return 0;
}
cout << ans[i];
}
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int N, M;
cin >> N >> M;
if (M == 0) {
if (N == 1)
cout << 0 << endl;
else if (N == 2)
cout << 10 << endl;
else
cout << 100 << endl;
return 0;
}
vector<int> s(M), c(M);
for (int i = 0; i < M; i++)
cin >> s[i] >> c[i];
if (N == 1) {
for (int i = 1; i < M; i++) {
if (c[i] != c[0]) {
cout << -1 << endl;
return 0;
}
}
cout << c[0] << endl;
return 0;
}
vector<int> ans(N, -1);
for (int i = 0; i < M; i++) {
if (ans[s[i] - 1] == -1)
ans[s[i] - 1] = c[i];
else {
if (ans[s[i] - 1] != c[i]) {
cout << -1 << endl;
return 0;
}
}
}
for (int i = 0; i < N; i++) {
if (ans[i] == -1) {
if (i == 0)
ans[i] = 1;
else
ans[i] = 0;
}
if (ans[0] == 0) {
cout << -1 << endl;
return 0;
}
cout << ans[i];
}
cout << endl;
return 0;
} | insert | 7 | 7 | 7 | 16 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 1e9 + 7;
const int64 infll = (1LL << 58) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64> vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
template <typename F> struct FixPoint : F {
FixPoint(F &&f) : F(forward<F>(f)) {}
template <typename... Args> decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
template <typename F> inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{forward<F>(f)};
}
int main() {
int N, M;
cin >> N >> M;
vector<int> t(M, -1);
for (int i = 0; i < M; i++) {
int k, x;
cin >> k >> x;
--k;
if (t[k] == -1 || t[k] == x) {
t[k] = x;
} else {
cout << -1 << endl;
return 0;
}
}
if (N == 1) {
for (auto &x : t)
cout << max(x, 0);
cout << endl;
} else {
if (t[0] == 0) {
cout << -1 << endl;
return 0;
}
if (t[0] == -1)
t[0] = 1;
for (auto &x : t)
cout << max(x, 0);
cout << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 1e9 + 7;
const int64 infll = (1LL << 58) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64> vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
template <typename F> struct FixPoint : F {
FixPoint(F &&f) : F(forward<F>(f)) {}
template <typename... Args> decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
template <typename F> inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{forward<F>(f)};
}
int main() {
int N, M;
cin >> N >> M;
vector<int> t(N, -1);
for (int i = 0; i < M; i++) {
int k, x;
cin >> k >> x;
--k;
if (t[k] == -1 || t[k] == x) {
t[k] = x;
} else {
cout << -1 << endl;
return 0;
}
}
if (N == 1) {
for (auto &x : t)
cout << max(x, 0);
cout << endl;
} else {
if (t[0] == 0) {
cout << -1 << endl;
return 0;
}
if (t[0] == -1)
t[0] = 1;
for (auto &x : t)
cout << max(x, 0);
cout << endl;
}
}
| replace | 86 | 87 | 86 | 87 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// #include <boost/algorithm/string/join.hpp>
// using boost::algorithm::join;
//
// #include "boost/multi_array.hpp"
// boost::multi_array<int, 3> dp;
// dp.resize(boost::extents[10][9][8]);
//
// #include <boost/multiprecision/cpp_dec_float.hpp> // quite slowly
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision; // mp::log10
// using bigint = boost::multiprecision::cpp_int;
// using rational = mp::cpp_rational; // double d = static_cast<double>(r);
typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;
// constexpr ll MOD = 1e9+7;
template <typename T> constexpr auto inf = numeric_limits<T>::max() / 4;
constexpr int INF = inf<int>;
constexpr ll LINF = inf<ll>;
#define Sort(x) sort(begin(x), end(x))
#define Reverse(x) reverse(begin(x), end(x))
#define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
#define nbit(n) (1ull << (n))
#define _ol3(_1, _2, _3, name, ...) name
#define _rep(i, n) _repi(i, 0, n)
#define _repi(i, a, b) for (int i = a, i##_l = (b); i < i##_l; ++i)
#define REP(...) _ol3(__VA_ARGS__, _repi, _rep, )(__VA_ARGS__)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define REPA(i, v) REP(i, (v).size())
// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置
// Returns minimum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmin(T &a, T b) {
if (a > b) {
a = b;
}
return a;
}
// Returns maximum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmax(T &a, T b) {
if (a < b) {
a = b;
}
return a;
}
template <typename T> void dumpContents(const T &v, const string &msg) {
cerr << "### " << msg << " ###\n";
for (const auto &x : v) {
cerr << x << " ";
}
cerr << endl;
}
struct before_main_function {
before_main_function() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(20) << fixed;
#define endl "\n"
}
} before_main_function;
// ========== end of template ==========
vii R;
bool check(int x) {
vi v;
while (x > 0) {
v.emplace_back(x % 10);
x /= 10;
}
Reverse(v);
for (auto p : R) {
if (v[p.first - 1] != p.second)
return false;
}
return true;
}
int main(int argc, char **argv) {
int N, M;
cin >> N >> M;
REP(i, M) {
int s, c;
cin >> s >> c;
R.emplace_back(s, c);
}
int imax = 1;
REP(i, N) { imax *= 10; }
int imin = imax / 10;
if (imin == 1)
imin = 0;
REP(i, imin, imax) {
if (check(i)) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
/* vim: set expandtab shiftwidth=2 softtabstop=2 : */
| #include <bits/stdc++.h>
using namespace std;
// #include <boost/algorithm/string/join.hpp>
// using boost::algorithm::join;
//
// #include "boost/multi_array.hpp"
// boost::multi_array<int, 3> dp;
// dp.resize(boost::extents[10][9][8]);
//
// #include <boost/multiprecision/cpp_dec_float.hpp> // quite slowly
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision; // mp::log10
// using bigint = boost::multiprecision::cpp_int;
// using rational = mp::cpp_rational; // double d = static_cast<double>(r);
typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;
// constexpr ll MOD = 1e9+7;
template <typename T> constexpr auto inf = numeric_limits<T>::max() / 4;
constexpr int INF = inf<int>;
constexpr ll LINF = inf<ll>;
#define Sort(x) sort(begin(x), end(x))
#define Reverse(x) reverse(begin(x), end(x))
#define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
#define nbit(n) (1ull << (n))
#define _ol3(_1, _2, _3, name, ...) name
#define _rep(i, n) _repi(i, 0, n)
#define _repi(i, a, b) for (int i = a, i##_l = (b); i < i##_l; ++i)
#define REP(...) _ol3(__VA_ARGS__, _repi, _rep, )(__VA_ARGS__)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define REPA(i, v) REP(i, (v).size())
// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置
// Returns minimum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmin(T &a, T b) {
if (a > b) {
a = b;
}
return a;
}
// Returns maximum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmax(T &a, T b) {
if (a < b) {
a = b;
}
return a;
}
template <typename T> void dumpContents(const T &v, const string &msg) {
cerr << "### " << msg << " ###\n";
for (const auto &x : v) {
cerr << x << " ";
}
cerr << endl;
}
struct before_main_function {
before_main_function() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(20) << fixed;
#define endl "\n"
}
} before_main_function;
// ========== end of template ==========
vii R;
bool check(int x) {
vi v;
if (x == 0) {
v.emplace_back(0);
} else {
while (x > 0) {
v.emplace_back(x % 10);
x /= 10;
}
}
Reverse(v);
for (auto p : R) {
if (v[p.first - 1] != p.second)
return false;
}
return true;
}
int main(int argc, char **argv) {
int N, M;
cin >> N >> M;
REP(i, M) {
int s, c;
cin >> s >> c;
R.emplace_back(s, c);
}
int imax = 1;
REP(i, N) { imax *= 10; }
int imin = imax / 10;
if (imin == 1)
imin = 0;
REP(i, imin, imax) {
if (check(i)) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
/* vim: set expandtab shiftwidth=2 softtabstop=2 : */
| replace | 93 | 96 | 93 | 100 | 0 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
// ABC157_C
int main() {
int N, M;
cin >> N >> M;
vector<int> S(N);
vector<int> C(N);
for (int i = 0; i < M; i++) {
cin >> S.at(i) >> C.at(i);
}
rep(i, 1000) {
string str_num = to_string(i);
if (str_num.length() != N)
continue;
bool flg = true;
rep(j, M) {
if (str_num[S.at(j) - 1] != C.at(j) + '0') {
flg = false;
}
}
if (flg) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
// ABC157_C
int main() {
int N, M;
cin >> N >> M;
vector<int> S(M);
vector<int> C(M);
for (int i = 0; i < M; i++) {
cin >> S.at(i) >> C.at(i);
}
rep(i, 1000) {
string str_num = to_string(i);
if (str_num.length() != N)
continue;
bool flg = true;
rep(j, M) {
if (str_num[S.at(j) - 1] != C.at(j) + '0') {
flg = false;
}
}
if (flg) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
} | replace | 21 | 23 | 21 | 23 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<int> vec(3, -1);
rep(i, n) {
int d, v;
cin >> d >> v;
if (vec[d - 1] != -1 && vec[d - 1] != v) {
cout << -1 << endl;
return 0;
}
vec[d - 1] = v;
}
if (n != 1 && vec[0] == 0) {
cout << -1 << endl;
return 0;
}
if (n == 1) {
int v = vec[0] != -1 ? vec[0] : 0;
cout << v << endl;
return 0;
}
int ans = 0;
int k = n - 1;
rep(i, n) {
int v = (vec[i] != -1) ? vec[i] : (i == 0 ? 1 : 0);
ans += v * pow(10, k);
k--;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<int> vec(3, -1);
rep(i, m) {
int d, v;
cin >> d >> v;
if (vec[d - 1] != -1 && vec[d - 1] != v) {
cout << -1 << endl;
return 0;
}
vec[d - 1] = v;
}
if (n != 1 && vec[0] == 0) {
cout << -1 << endl;
return 0;
}
if (n == 1) {
int v = vec[0] != -1 ? vec[0] : 0;
cout << v << endl;
return 0;
}
int ans = 0;
int k = n - 1;
rep(i, n) {
int v = (vec[i] != -1) ? vec[i] : (i == 0 ? 1 : 0);
ans += v * pow(10, k);
k--;
}
cout << ans << endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
std::vector<int> split(int x) {
if (x == 0)
return {0};
std::vector<int> ans;
while (x)
ans.push_back(x % 10), x /= 10;
std::reverse(ans.begin(), ans.end());
return ans;
}
int main() {
int n, m;
std::cin >> n >> m;
std::vector<std::pair<int, int>> condition;
for (int i = 0; i < m; ++i)
std::cin >> condition[i].first >> condition[i].second;
for (int ans = 0; ans <= 999; ++ans) {
auto res = split(ans);
if (res.size() != n)
continue;
bool flag = true;
for (int i = 0; i < m; ++i)
if (res[condition[i].first - 1] != condition[i].second)
flag = false;
if (!flag)
continue;
std::cout << ans << "\n";
return 0;
}
std::cout << -1 << "\n";
}
| #include <algorithm>
#include <iostream>
#include <vector>
std::vector<int> split(int x) {
if (x == 0)
return {0};
std::vector<int> ans;
while (x)
ans.push_back(x % 10), x /= 10;
std::reverse(ans.begin(), ans.end());
return ans;
}
int main() {
int n, m;
std::cin >> n >> m;
std::vector<std::pair<int, int>> condition(m);
for (int i = 0; i < m; ++i)
std::cin >> condition[i].first >> condition[i].second;
for (int ans = 0; ans <= 999; ++ans) {
auto res = split(ans);
if (res.size() != n)
continue;
bool flag = true;
for (int i = 0; i < m; ++i)
if (res[condition[i].first - 1] != condition[i].second)
flag = false;
if (!flag)
continue;
std::cout << ans << "\n";
return 0;
}
std::cout << -1 << "\n";
}
| replace | 17 | 18 | 17 | 18 | -11 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// 0 <= unsigned int <= 4.2e9
int main() {
size_t N, M;
cin >> N >> M;
string S(N, '0');
if (N > 1) {
S[0] = 1;
}
vector<int> c(N + 1, -1);
for (size_t i = 0; i < M; i++) {
size_t si;
int temp;
cin >> si >> temp;
if (c[si] == -1) {
c[si] = temp;
} else {
if (c[si] != temp) {
cout << -1 << endl;
return 0;
} else {
c[si] = temp;
}
}
string ss = to_string(temp);
S[si - 1] = ss[0];
}
size_t len = S.length();
if ((S.length() > 1) && (S[0] == '0')) {
cout << -1 << endl;
return 0;
}
int ans = stoi(S);
cout << ans << endl;
bool check = true;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// 0 <= unsigned int <= 4.2e9
int main() {
size_t N, M;
cin >> N >> M;
string S(N, '0');
if (N > 1) {
S[0] = '1';
}
vector<int> c(N + 1, -1);
for (size_t i = 0; i < M; i++) {
size_t si;
int temp;
cin >> si >> temp;
if (c[si] == -1) {
c[si] = temp;
} else {
if (c[si] != temp) {
cout << -1 << endl;
return 0;
} else {
c[si] = temp;
}
}
string ss = to_string(temp);
S[si - 1] = ss[0];
}
size_t len = S.length();
if ((S.length() > 1) && (S[0] == '0')) {
cout << -1 << endl;
return 0;
}
int ans = stoi(S);
cout << ans << endl;
bool check = true;
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int main() {
int N, M;
cin >> N >> M;
string num(M, '.');
for (int i = 0; i < M; i++) {
int c, s;
cin >> s >> c;
if (N > 1 && s == 1 && c == 0) {
cout << -1 << endl;
return 0;
}
if (num[s - 1] != '.' && num[s - 1] != (char)(c + '0')) {
cout << -1 << endl;
return 0;
}
num[s - 1] = (char)(c + '0');
}
for (char &ch : num) {
if (ch == '.') {
ch = '0';
}
}
if (num[0] == '0') {
if (N == 1) {
cout << 0 << endl;
return 0;
} else {
}
num[0] = '1';
}
int min_num = stoi(num);
cout << min_num << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int main() {
int N, M;
cin >> N >> M;
string num(N, '.');
for (int i = 0; i < M; i++) {
int c, s;
cin >> s >> c;
if (N > 1 && s == 1 && c == 0) {
cout << -1 << endl;
return 0;
}
if (num[s - 1] != '.' && num[s - 1] != (char)(c + '0')) {
cout << -1 << endl;
return 0;
}
num[s - 1] = (char)(c + '0');
}
for (char &ch : num) {
if (ch == '.') {
ch = '0';
}
}
if (num[0] == '0') {
if (N == 1) {
cout << 0 << endl;
return 0;
} else {
}
num[0] = '1';
}
int min_num = stoi(num);
cout << min_num << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
#define cout16 cout << setprecision(16)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())
#define SORT(A) sort(A.begin(), A.end())
#define REV(A) reverse(A.begin(), A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int, int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second
int main(void) {
int n, m;
cin >> n >> m;
vp sc(m);
rep(i, m) {
int s, c;
cin >> s >> c;
s--;
sc[i].fi = s;
sc[i].se = c;
}
SORT(sc);
rep(i, m - 1) {
if (sc[i].fi == sc[i + 1].fi) {
if (sc[i].se != sc[i + 1].se) {
cout << -1 << endl;
return 0;
}
}
}
vi ans(n, 0);
rep(i, m) ans[sc[i].fi] = sc[i].se;
if (sc[0].fi == 0 && sc[0].se == 0) {
if (n == 1) {
cout << 0 << endl;
return 0;
} else {
cout << -1 << endl;
return 0;
}
}
if (ans[0] == 0)
ans[0] = 1;
rep(i, n) cout << ans[i];
cout << endl;
}
| #include <bits/stdc++.h>
#define cout16 cout << setprecision(16)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())
#define SORT(A) sort(A.begin(), A.end())
#define REV(A) reverse(A.begin(), A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int, int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second
int main(void) {
int n, m;
cin >> n >> m;
if (m == 0) {
if (n == 1) {
cout << 0;
} else {
cout << 1;
rep(i, n - 1) cout << 0;
}
cout << endl;
return 0;
}
vp sc(m);
rep(i, m) {
int s, c;
cin >> s >> c;
s--;
sc[i].fi = s;
sc[i].se = c;
}
SORT(sc);
rep(i, m - 1) {
if (sc[i].fi == sc[i + 1].fi) {
if (sc[i].se != sc[i + 1].se) {
cout << -1 << endl;
return 0;
}
}
}
vi ans(n, 0);
rep(i, m) ans[sc[i].fi] = sc[i].se;
if (sc[0].fi == 0 && sc[0].se == 0) {
if (n == 1) {
cout << 0 << endl;
return 0;
} else {
cout << -1 << endl;
return 0;
}
}
if (ans[0] == 0)
ans[0] = 1;
rep(i, n) cout << ans[i];
cout << endl;
}
| insert | 24 | 24 | 24 | 34 | 0 | |
p02761 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
int main() {
int n, m;
cin >> n >> m;
vector<P> p(m);
rep(i, m) cin >> p[i].first >> p[i].second;
rep(x, 1000) {
int keta = 1;
int nx = x / 10;
vector<int> d(1, x % 10);
while (nx) {
keta++;
d.push_back(nx % 10);
nx /= 10;
}
if (keta != n)
continue;
bool ok = true;
reverse(d.begin(), d.end());
rep(i, n) {
if (d[p[i].first - 1] != p[i].second)
ok = false;
}
if (ok) {
cout << x << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
int main() {
int n, m;
cin >> n >> m;
vector<P> p(m);
rep(i, m) cin >> p[i].first >> p[i].second;
rep(x, 1000) {
int keta = 1;
int nx = x / 10;
vector<int> d(1, x % 10);
while (nx) {
keta++;
d.push_back(nx % 10);
nx /= 10;
}
if (keta != n)
continue;
bool ok = true;
reverse(d.begin(), d.end());
rep(i, m) {
if (d[p[i].first - 1] != p[i].second)
ok = false;
}
if (ok) {
cout << x << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| replace | 25 | 26 | 25 | 26 | 0 | |
p02761 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s[i] >> c[i];
}
int st = 1;
for (int i = 1; i < N; i++) {
st *= 10;
}
int ed = st * 10;
if (N == 1) {
st = 0;
}
int ans = -1;
for (int i = st; i < ed; i++) {
vector<int> a;
int tmp = i;
while (tmp != 0) {
a.push_back(tmp % 10);
tmp /= 10;
}
int flag = 1;
for (int j = 0; j < M; j++) {
if (a[N - s[j]] != c[j]) {
flag = 0;
break;
}
}
if (flag) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
using ll = long long;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> s(M);
vector<int> c(M);
for (int i = 0; i < M; i++) {
cin >> s[i] >> c[i];
}
int st = 1;
for (int i = 1; i < N; i++) {
st *= 10;
}
int ed = st * 10;
if (N == 1) {
st = 0;
}
int ans = -1;
for (int i = st; i < ed; i++) {
vector<int> a;
int tmp = i;
if (tmp == 0) {
a.push_back(0);
}
while (tmp != 0) {
a.push_back(tmp % 10);
tmp /= 10;
}
int flag = 1;
for (int j = 0; j < M; j++) {
if (a[N - s[j]] != c[j]) {
flag = 0;
break;
}
}
if (flag) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
} | insert | 27 | 27 | 27 | 30 | 0 | |
p02761 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m), c(m), a(n + 1);
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
a[s[i]] = c[i];
}
for (int i = 0; i < m; i++) {
if (a[s[i]] != c[i] || (n > 1 && s[i] == 1 && c[i] == 0)) {
cout << -1;
return 0;
}
}
if (s[1] == 0 && n > 1)
s[1] = 1;
for (int i = 1; i <= n; i++)
cout << a[i];
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> s(m), c(m), a(n + 1);
for (int i = 0; i < m; i++) {
cin >> s[i] >> c[i];
a[s[i]] = c[i];
}
for (int i = 0; i < m; i++) {
if (a[s[i]] != c[i] || (n > 1 && s[i] == 1 && c[i] == 0)) {
cout << -1;
return 0;
}
}
if (a[1] == 0 && n > 1)
a[1] = 1;
for (int i = 1; i <= n; i++)
cout << a[i];
} | replace | 18 | 20 | 18 | 20 | 0 | |
p02761 | Python | Runtime Error | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
n, m = map(int, readline().split())
if m == 0:
if n == 1:
print(0)
else:
print("1" + "0" * (n - 1))
sc = [list(map(int, readline().split())) for i in range(m)]
sc.sort()
memo = sc[0]
if sc[0] == [1, 0] and n != 1:
print(-1)
exit()
for i in range(1, m):
if memo[0] == sc[i][0]:
if memo[1] != sc[i][1]:
print(-1)
exit()
else:
memo = sc[i]
ans = ""
for i in range(n):
for j in range(m):
if i + 1 == sc[j][0]:
ans += str(sc[j][1])
break
if len(ans) != i + 1:
if len(ans) == 0:
ans += "1"
else:
ans += "0"
print(ans)
| import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
n, m = map(int, readline().split())
if m == 0:
if n == 1:
print(0)
else:
print("1" + "0" * (n - 1))
exit()
sc = [list(map(int, readline().split())) for i in range(m)]
sc.sort()
memo = sc[0]
if sc[0] == [1, 0] and n != 1:
print(-1)
exit()
for i in range(1, m):
if memo[0] == sc[i][0]:
if memo[1] != sc[i][1]:
print(-1)
exit()
else:
memo = sc[i]
ans = ""
for i in range(n):
for j in range(m):
if i + 1 == sc[j][0]:
ans += str(sc[j][1])
break
if len(ans) != i + 1:
if len(ans) == 0:
ans += "1"
else:
ans += "0"
print(ans)
| insert | 13 | 13 | 13 | 14 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define inf 1000000007
using namespace std;
int n, m, k, fa[100009], num[100009];
vector<int> w[100009];
vector<int> w1[100009];
int findl(int x) { return fa[x] == x ? x : findl(fa[x]); }
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
fa[i] = i, num[i] = 1;
for (int i = 0; i < m; i++) {
int x, y, tmp, tmp1;
scanf("%d%d", &x, &y);
w[x].push_back(y), w[y].push_back(x);
tmp = findl(x), tmp1 = findl(y);
if (tmp != tmp1)
fa[tmp] = tmp1, num[tmp1] += num[tmp];
}
for (int i = 0; i < k; i++) {
int x, y;
scanf("%d%d", &x, &y);
w1[x].push_back(y), w1[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
int ans = num[findl(i)] - w[i].size() - 1;
for (int j = 0; j < w1[i].size(); j++) {
int tmp = findl(i), tmp1 = findl(w1[i][j]);
if (tmp == tmp1)
ans--;
}
printf("%d ", ans);
}
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define inf 1000000007
using namespace std;
int n, m, k, fa[100009], num[100009];
vector<int> w[100009];
vector<int> w1[100009];
int findl(int x) { return fa[x] == x ? x : fa[x] = findl(fa[x]); }
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
fa[i] = i, num[i] = 1;
for (int i = 0; i < m; i++) {
int x, y, tmp, tmp1;
scanf("%d%d", &x, &y);
w[x].push_back(y), w[y].push_back(x);
tmp = findl(x), tmp1 = findl(y);
if (tmp != tmp1)
fa[tmp] = tmp1, num[tmp1] += num[tmp];
}
for (int i = 0; i < k; i++) {
int x, y;
scanf("%d%d", &x, &y);
w1[x].push_back(y), w1[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
int ans = num[findl(i)] - w[i].size() - 1;
for (int j = 0; j < w1[i].size(); j++) {
int tmp = findl(i), tmp1 = findl(w1[i][j]);
if (tmp == tmp1)
ans--;
}
printf("%d ", ans);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p02762 | Python | Runtime Error | class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = x, y
self.parents[x] += self.parents[y]
self.parents[y] = x
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -self.parents[self.find(x)]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
N, M, K = map(int, input().split())
uf = UnionFind(N)
friends = [[] for _ in range(N)]
blocks = [[] for _ in range(N)]
root = {}
for i in range(M):
a, b = map(int, input().split())
friends[a - 1].append(b - 1)
friends[b - 1].append(a - 1)
uf.unite(a - 1, b - 1)
roots = uf.roots()
for r in roots:
size = uf.size(r)
root[r] = size - 1
for i in range(K):
c, d = map(int, input().split())
if uf.same(c - 1, d - 1):
blocks[c - 1].append(d - 1)
blocks[d - 1].append(c - 1)
ans = []
for i in range(N):
ans.append(root[uf.find(i)] - len(blocks[i]) - len(friends[i]))
print(*ans)
| class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def unite(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 same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -self.parents[self.find(x)]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
N, M, K = map(int, input().split())
uf = UnionFind(N)
friends = [[] for _ in range(N)]
blocks = [[] for _ in range(N)]
root = {}
for i in range(M):
a, b = map(int, input().split())
friends[a - 1].append(b - 1)
friends[b - 1].append(a - 1)
uf.unite(a - 1, b - 1)
roots = uf.roots()
for r in roots:
size = uf.size(r)
root[r] = size - 1
for i in range(K):
c, d = map(int, input().split())
if uf.same(c - 1, d - 1):
blocks[c - 1].append(d - 1)
blocks[d - 1].append(c - 1)
ans = []
for i in range(N):
ans.append(root[uf.find(i)] - len(blocks[i]) - len(friends[i]))
print(*ans)
| replace | 17 | 18 | 17 | 18 | 0 | |
p02762 | Python | Runtime Error | # https://atcoder.jp/contests/abc157/submissions/10475457
import sys
readline = sys.stdin.readine
N, M, K = map(int, readline().split())
F = [[] for _ in range(N + 1)]
for _ in range(M):
a, b = map(int, readline().split())
F[a].append(b)
F[b].append(a)
B = [[] for _ in range(N + 1)]
for _ in range(K):
c, d = map(int, readline().split())
B[c].append(d)
B[d].append(c)
seen = [-1] * (N + 1)
group = 0
def dfs(graph, s):
stack = [s]
seen[s] = group
n_connection = 1
while stack:
u = stack.pop()
for v in graph[u]:
if seen[v] == -1:
n_connection += 1
seen[v] = group
stack.append(v)
return n_connection
union = {}
for i in range(1, N + 1):
if seen[i] == -1:
n = dfs(F, i)
union[group] = n
group += 1
ans = []
for i in range(1, N + 1):
tmp = 0
tmp += union[seen[i]] - 1
tmp -= len(F[i])
for j in B[i]:
if seen[j] == seen[i]:
tmp -= 1
ans.append(tmp)
print(" ".join(map(str, ans)))
| # https://atcoder.jp/contests/abc157/submissions/10475457
import sys
readline = sys.stdin.readline
N, M, K = map(int, readline().split())
F = [[] for _ in range(N + 1)]
for _ in range(M):
a, b = map(int, readline().split())
F[a].append(b)
F[b].append(a)
B = [[] for _ in range(N + 1)]
for _ in range(K):
c, d = map(int, readline().split())
B[c].append(d)
B[d].append(c)
seen = [-1] * (N + 1)
group = 0
def dfs(graph, s):
stack = [s]
seen[s] = group
n_connection = 1
while stack:
u = stack.pop()
for v in graph[u]:
if seen[v] == -1:
n_connection += 1
seen[v] = group
stack.append(v)
return n_connection
union = {}
for i in range(1, N + 1):
if seen[i] == -1:
n = dfs(F, i)
union[group] = n
group += 1
ans = []
for i in range(1, N + 1):
tmp = 0
tmp += union[seen[i]] - 1
tmp -= len(F[i])
for j in B[i]:
if seen[j] == seen[i]:
tmp -= 1
ans.append(tmp)
print(" ".join(map(str, ans)))
| replace | 4 | 5 | 4 | 5 | AttributeError: '_io.TextIOWrapper' object has no attribute 'readine'. Did you mean: 'readline'? | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02762/Python/s918712039.py", line 4, in <module>
readline = sys.stdin.readine
AttributeError: '_io.TextIOWrapper' object has no attribute 'readine'. Did you mean: 'readline'?
|
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
void Main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> frig(n); // friend group tree
vector<int> numf(n); // number of friend
vector<int> numb(n); // number of ban
vector<int> numc(n); // size of group
for (int i = 0; i < n; i++) {
frig[i] = i;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
numf[a]++;
numf[b]++;
int p1 = a, p2 = b;
while (frig[p1] != p1)
p1 = frig[p1];
while (frig[p2] != p2)
p2 = frig[p2];
frig[p1] = p2;
}
for (int i = 0; i < n; i++) {
int p = i;
while (frig[p] != p)
p = frig[p];
frig[i] = p;
numc[p]++;
}
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
if (frig[c] == frig[d]) {
numb[c]++;
numb[d]++;
}
}
for (int i = 0; i < n; i++) {
if (i != 0)
cout << " ";
cout << numc[frig[i]] - numf[i] - numb[i] - 1;
}
cout << endl;
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
Main();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void Main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> frig(n); // friend group tree
vector<int> numf(n); // number of friend
vector<int> numb(n); // number of ban
vector<int> numc(n); // size of group
for (int i = 0; i < n; i++) {
frig[i] = i;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
numf[a]++;
numf[b]++;
int p1 = a, p2 = b;
while (frig[p1] != p1)
p1 = frig[p1];
while (frig[p2] != p2)
p2 = frig[p2];
frig[p1] = p2;
frig[a] = p2;
frig[b] = p2;
}
for (int i = 0; i < n; i++) {
int p = i;
while (frig[p] != p)
p = frig[p];
frig[i] = p;
numc[p]++;
}
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
if (frig[c] == frig[d]) {
numb[c]++;
numb[d]++;
}
}
for (int i = 0; i < n; i++) {
if (i != 0)
cout << " ";
cout << numc[frig[i]] - numf[i] - numb[i] - 1;
}
cout << endl;
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
Main();
return 0;
}
| insert | 27 | 27 | 27 | 29 | TLE | |
p02762 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"
struct UnionFind {
vi par, count;
UnionFind(int N) : par(N), count(N, 1) {
for (int i = 0; i < N; i++)
par.at(i) = i;
}
int root(int x) {
if (par.at(x) == x)
return x;
return par.at(x) = root(par.at(x));
}
int unite(int x, int y) {
if (root(x) == root(y))
return 0;
if (size(x) < size(y)) {
count.at(root(y)) += count.at(root(x));
par.at(root(x)) = root(y);
} else {
count.at(root(x)) += count.at(root(y));
par.at(root(y)) = root(x);
}
return 1;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return count.at(root(x)); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, M, K;
cin >> N >> M >> K;
UnionFind uf(N);
vll ans(N, 0LL);
for (int i = 0; i < M; i++) {
ll A, B;
cin >> A >> B;
A--;
B--;
uf.unite(A, B);
ans.at(A)--;
ans.at(B)--;
}
for (int i = 0; i < N; i++) {
ans.at(i) += uf.size(i) - 1;
}
vector<vector<bool>> blo(N, vector<bool>(N, false));
for (int i = 0; i < K; i++) {
ll C, D;
cin >> C >> D;
C--;
D--;
if (uf.same(C, D)) {
ans.at(C)--;
ans.at(D)--;
}
}
for (int i = 0; i < N; i++) {
cout << ans.at(i);
if (i == N - 1)
cout << endl;
else
cout << ' ';
}
} | #include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"
struct UnionFind {
vi par, count;
UnionFind(int N) : par(N), count(N, 1) {
for (int i = 0; i < N; i++)
par.at(i) = i;
}
int root(int x) {
if (par.at(x) == x)
return x;
return par.at(x) = root(par.at(x));
}
int unite(int x, int y) {
if (root(x) == root(y))
return 0;
if (size(x) < size(y)) {
count.at(root(y)) += count.at(root(x));
par.at(root(x)) = root(y);
} else {
count.at(root(x)) += count.at(root(y));
par.at(root(y)) = root(x);
}
return 1;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return count.at(root(x)); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, M, K;
cin >> N >> M >> K;
UnionFind uf(N);
vll ans(N, 0LL);
for (int i = 0; i < M; i++) {
ll A, B;
cin >> A >> B;
A--;
B--;
uf.unite(A, B);
ans.at(A)--;
ans.at(B)--;
}
for (int i = 0; i < N; i++) {
ans.at(i) += uf.size(i) - 1;
}
for (int i = 0; i < K; i++) {
ll C, D;
cin >> C >> D;
C--;
D--;
if (uf.same(C, D)) {
ans.at(C)--;
ans.at(D)--;
}
}
for (int i = 0; i < N; i++) {
cout << ans.at(i);
if (i == N - 1)
cout << endl;
else
cout << ' ';
}
} | delete | 84 | 85 | 84 | 84 | MLE | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FILE
#define fr first
#define se second
using namespace std;
const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;
int n;
int m;
int k;
int x;
int y;
int c;
vector<int> g[N];
vector<int> num(N, 0);
vector<int> comp(N, -1);
void dfs(int v, int c) {
if (comp[v] != c) {
return;
}
comp[v] = c;
for (auto to : g[v]) {
dfs(to, c);
}
}
int main() {
#ifdef FILEs
freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (comp[i] == -1) {
dfs(i, ++c);
}
}
for (int i = 1; i <= n; i++) {
num[comp[i]]++;
}
vector<int> res(n + 1, 0);
for (int i = 1; i <= n; i++) {
res[i] = num[comp[i]] - 1 - (int)g[i].size();
}
for (int i = 1; i <= k; i++) {
cin >> x >> y;
if (comp[x] == comp[y]) {
res[x]--, res[y]--;
}
}
for (int i = 1; i <= n; i++) {
cout << res[i] << " ";
}
cout << "\n";
}
| #include <bits/stdc++.h>
#define FILE
#define fr first
#define se second
using namespace std;
const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;
int n;
int m;
int k;
int x;
int y;
int c;
vector<int> g[N];
vector<int> num(N, 0);
vector<int> comp(N, -1);
void dfs(int v, int c) {
if (comp[v] != -1) {
return;
}
comp[v] = c;
for (auto to : g[v]) {
dfs(to, c);
}
}
int main() {
#ifdef FILEs
freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (comp[i] == -1) {
dfs(i, ++c);
}
}
for (int i = 1; i <= n; i++) {
num[comp[i]]++;
}
vector<int> res(n + 1, 0);
for (int i = 1; i <= n; i++) {
res[i] = num[comp[i]] - 1 - (int)g[i].size();
}
for (int i = 1; i <= k; i++) {
cin >> x >> y;
if (comp[x] == comp[y]) {
res[x]--, res[y]--;
}
}
for (int i = 1; i <= n; i++) {
cout << res[i] << " ";
}
cout << "\n";
}
| replace | 22 | 23 | 22 | 23 | -11 | |
p02762 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define mfill(x, y) memset(x, y, sizeof(x))
#define all(v) v.begin(), v.end()
#define in(x, y, h, w) if (0 <= x && x < h && 0 <= y && y < w)
#define y0 y12345
#define y1 y54321
#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#endif
using ul = unsigned long;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
template <class T> T gcd(T &a, T &b) {
if (a < b) {
swap(a, b);
}
T r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1};
ll n, m, k;
vvint a, b;
vint d;
int namer = 0;
vvint mem;
int dfs(int par) {
d[par] = namer;
mem[namer].push_back(par);
int ret = 1;
rep(i, a[par].size()) {
if (d[a[par][i]] == -1) {
ret += dfs(a[par][i]);
}
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
debug("debug test\n");
cin >> n >> m >> k;
a = vector<vint>(n);
b = vector<vint>(n);
d = vint(n, -1);
rep(i, m) {
int p, q;
cin >> p >> q;
a[p - 1].push_back(q - 1);
a[q - 1].push_back(p - 1);
}
rep(i, k) {
int p, q;
cin >> p >> q;
b[p - 1].push_back(q - 1);
b[q - 1].push_back(p - 1);
}
// sort();
vint nl;
rep(i, n) {
if (d[i] == -1) {
mem.push_back(vint());
nl.push_back(dfs(i));
sort(all(mem[namer]));
namer++;
}
}
rep(i, namer) {
debug("i: %d\n", i);
rep(j, nl[i]) { debug("%d %d\n", j, mem[i][j]); }
}
debug("\n");
rep(i, n) {
if (i != 0)
cout << ' ';
int bcou = 0;
rep(j, b[i].size()) {
if (binary_search(all(mem[d[i]]), b[i][j])) {
bcou++;
}
}
cout << nl[d[i]] - a[i].size() - 1 - bcou;
}
cout << endl;
return 0;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define mfill(x, y) memset(x, y, sizeof(x))
#define all(v) v.begin(), v.end()
#define in(x, y, h, w) if (0 <= x && x < h && 0 <= y && y < w)
#define y0 y12345
#define y1 y54321
#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#endif
using ul = unsigned long;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
template <class T> T gcd(T &a, T &b) {
if (a < b) {
swap(a, b);
}
T r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
vint dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
vint dx8 = {-1, -1, -1, 0, 1, 1, 1, 0}, dy8 = {-1, 0, 1, 1, 1, 0, -1, -1};
ll n, m, k;
vvint a, b;
vint d;
int namer = 0;
vvint mem;
int dfs(int par) {
d[par] = namer;
mem[namer].push_back(par);
int ret = 1;
rep(i, a[par].size()) {
if (d[a[par][i]] == -1) {
ret += dfs(a[par][i]);
}
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
debug("debug test\n");
cin >> n >> m >> k;
a = vector<vint>(n);
b = vector<vint>(n);
d = vint(n, -1);
rep(i, m) {
int p, q;
cin >> p >> q;
a[p - 1].push_back(q - 1);
a[q - 1].push_back(p - 1);
}
rep(i, k) {
int p, q;
cin >> p >> q;
b[p - 1].push_back(q - 1);
b[q - 1].push_back(p - 1);
}
// sort();
vint nl;
rep(i, n) {
if (d[i] == -1) {
mem.push_back(vint());
nl.push_back(dfs(i));
sort(all(mem[namer]));
namer++;
}
}
rep(i, namer) {
debug("i: %d\n", i);
rep(j, nl[i]) { debug("%d %d\n", j, mem[i][j]); }
}
debug("\n");
rep(i, n) {
if (i != 0)
cout << ' ';
int bcou = 0;
rep(j, b[i].size()) {
if (d[i] == d[b[i][j]]) {
// if(binary_search(all(mem[d[i]]), b[i][j])){
bcou++;
}
}
cout << nl[d[i]] - a[i].size() - 1 - bcou;
}
cout << endl;
return 0;
}
| replace | 115 | 116 | 115 | 117 | TLE | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
struct UnionFind {
vector<int> d; // 親の番号、但し根の場合はサイズのマイナス
UnionFind(int n) : d(n, -1) {}
int root(int x) { // 根を見つける
if (d[x] < 0)
return x;
return d[x] = root(d[x]); // 経路圧縮
}
bool unite(int x, int y) { // 結合
x = root(x);
y = root(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); } // 親が一緒
int size(int x) { return -d[root(x)]; } // サイズ
};
int main() {
int N, M, K;
cin >> N >> M >> K;
UnionFind uf(N);
int a, b;
vector<vector<int>> A(N);
rep(i, M) {
cin >> a >> b;
a--;
b--;
uf.unite(a, b);
A[a].push_back(b);
A[b].push_back(a);
}
rep(i, K) {
cin >> a >> b;
a--;
b--;
A[a].push_back(b);
A[b].push_back(a);
}
rep(i, N) {
int ans = 0;
rep(j, N) {
if (i == j)
continue;
if (uf.same(i, j)) {
bool b = true;
for (int a : A[i]) {
b &= (a != j);
}
if (b)
ans++;
}
}
cout << ans << " ";
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
struct UnionFind {
vector<int> d; // 親の番号、但し根の場合はサイズのマイナス
UnionFind(int n) : d(n, -1) {}
int root(int x) { // 根を見つける
if (d[x] < 0)
return x;
return d[x] = root(d[x]); // 経路圧縮
}
bool unite(int x, int y) { // 結合
x = root(x);
y = root(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); } // 親が一緒
int size(int x) { return -d[root(x)]; } // サイズ
};
int main() {
int N, M, K;
cin >> N >> M >> K;
UnionFind uf(N);
int a, b;
vector<vector<int>> A(N);
rep(i, M) {
cin >> a >> b;
a--;
b--;
uf.unite(a, b);
A[a].push_back(b);
A[b].push_back(a);
}
rep(i, K) {
cin >> a >> b;
a--;
b--;
A[a].push_back(b);
A[b].push_back(a);
}
rep(i, N) {
int ans = uf.size(i) - 1;
for (int a : A[i]) {
if (uf.same(i, a))
ans--;
}
cout << ans << " ";
}
cout << endl;
return 0;
}
| replace | 64 | 76 | 64 | 68 | TLE | |
p02762 | Python | Runtime Error | # 同じ木に属する(==そこまでたどりつけるかどうか)の判定にunionfind
# 同じ木に属しているノードはあとで数えることができる(そうだね)
# 除外するやつが出てくる。直の友達+ブロック関係にあるやつ(と自分)
# 同じ木に属しているノードの総数 - (直の友達+ブロック関係+自分自身)が答え
n, m, k = map(int, input().split())
par = [i for i in range(n)]
ans = [0 for _ in range(n)]
def root(x):
if x == par[x]:
return x
y = root(par[x])
par[x] = y
return y
def union(x, y):
if x > y:
x, y = y, x
rx, ry = root(x), root(y)
if rx != ry:
par[ry] = rx
friends = [[] for _ in range(n)]
blocks = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
union(a - 1, b - 1)
friends[a - 1].append(b - 1)
friends[b - 1].append(a - 1)
for _ in range(k):
c, d = map(int, input().split())
blocks[c - 1].append(d - 1)
blocks[d - 1].append(c - 1)
# 木に属しているやつを数えるぜ
group_count = [0 for _ in range(n)]
for i in range(n):
group_count[root(i)] += 1
# 答えを作るぜ
for i in range(n):
# 初期化(じゃないけど)
ans[i] += group_count[root(i)]
# 直の友達を除く
ans[i] -= len(friends[i])
# ブロックしてるやつを除く
for b in blocks[i]:
ra, rb = root(i), root(b)
if ra == rb:
ans[i] -= 1
# 自分自身を除く
ans[i] -= 1
print(" ".join(list(map(str, ans))))
| # 同じ木に属する(==そこまでたどりつけるかどうか)の判定にunionfind
# 同じ木に属しているノードはあとで数えることができる(そうだね)
# 除外するやつが出てくる。直の友達+ブロック関係にあるやつ(と自分)
# 同じ木に属しているノードの総数 - (直の友達+ブロック関係+自分自身)が答え
from sys import setrecursionlimit
setrecursionlimit(10**8)
n, m, k = map(int, input().split())
par = [i for i in range(n)]
ans = [0 for _ in range(n)]
def root(x):
if x == par[x]:
return x
y = root(par[x])
par[x] = y
return y
def union(x, y):
if x > y:
x, y = y, x
rx, ry = root(x), root(y)
if rx != ry:
par[ry] = rx
friends = [[] for _ in range(n)]
blocks = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
union(a - 1, b - 1)
friends[a - 1].append(b - 1)
friends[b - 1].append(a - 1)
for _ in range(k):
c, d = map(int, input().split())
blocks[c - 1].append(d - 1)
blocks[d - 1].append(c - 1)
# 木に属しているやつを数えるぜ
group_count = [0 for _ in range(n)]
for i in range(n):
group_count[root(i)] += 1
# 答えを作るぜ
for i in range(n):
# 初期化(じゃないけど)
ans[i] += group_count[root(i)]
# 直の友達を除く
ans[i] -= len(friends[i])
# ブロックしてるやつを除く
for b in blocks[i]:
ra, rb = root(i), root(b)
if ra == rb:
ans[i] -= 1
# 自分自身を除く
ans[i] -= 1
print(" ".join(list(map(str, ans))))
| insert | 4 | 4 | 4 | 7 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
void textIO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void fastIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
struct ps {
long long par, sz;
};
#define N 1e5 + 5
vector<ps> par(N);
long long Find(long long node) {
return par[node].par == node ? node : par[node].par = Find(par[node].par);
}
bool Union(long long u, long long v) {
long long pu = Find(u);
long long pv = Find(v);
if (pu != pv) {
par[pu].par = pv;
par[pv].sz += par[pu].sz;
par[pu].sz = 1;
return true;
}
return false;
}
vector<int> arr[100005];
int main() {
textIO();
fastIO();
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
par[i].par = i;
par[i].sz = 1;
}
for (int i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
Union(u, v);
arr[u].push_back(v);
arr[v].push_back(u);
}
for (int i = 0; i < k; i++) {
long long u, v;
cin >> u >> v;
if (Find(u) == Find(v)) {
arr[u].push_back(v);
arr[v].push_back(u);
}
}
for (int i = 1; i <= n; i++) {
cout << par[Find(i)].sz - arr[i].size() - 1 << " ";
}
cout << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void textIO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void fastIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
struct ps {
long long par, sz;
};
#define N 1e5 + 5
vector<ps> par(N);
long long Find(long long node) {
return par[node].par == node ? node : par[node].par = Find(par[node].par);
}
bool Union(long long u, long long v) {
long long pu = Find(u);
long long pv = Find(v);
if (pu != pv) {
par[pu].par = pv;
par[pv].sz += par[pu].sz;
par[pu].sz = 1;
return true;
}
return false;
}
vector<int> arr[100005];
int main() {
// textIO();
fastIO();
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
par[i].par = i;
par[i].sz = 1;
}
for (int i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
Union(u, v);
arr[u].push_back(v);
arr[v].push_back(u);
}
for (int i = 0; i < k; i++) {
long long u, v;
cin >> u >> v;
if (Find(u) == Find(v)) {
arr[u].push_back(v);
arr[v].push_back(u);
}
}
for (int i = 1; i <= n; i++) {
cout << par[Find(i)].sz - arr[i].size() - 1 << " ";
}
cout << '\n';
return 0;
}
| replace | 39 | 40 | 39 | 40 | -11 | |
p02762 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define sqr(x) ((x) * (x))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
typedef long long ll;
const int MOD = 1000000007;
const int INF = 1000000007;
const ll INFLL = 1000000000000000007LL;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N, -1) {}
int root(int x) {
if (par[x] < 0)
return x;
return root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] += par[ry];
par[ry] = rx;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -par[root(x)]; }
};
void solve() {
int N, M, K;
cin >> N >> M >> K;
UnionFind uf(N);
vector<int> deg(N, 0);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
deg[a]++;
deg[b]++;
uf.unite(a, b);
}
vector<vector<int>> block(N);
for (int i = 0; i < K; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
block[a].push_back(b);
block[b].push_back(a);
}
for (int i = 0; i < N; i++) {
int ans = uf.size(i) - deg[i] - 1;
for (int b : block[i]) {
if (uf.same(i, b))
ans--;
}
cout << ans << ' ';
}
cout << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// cout << fixed << setprecision(12);
solve();
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define sqr(x) ((x) * (x))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
typedef long long ll;
const int MOD = 1000000007;
const int INF = 1000000007;
const ll INFLL = 1000000000000000007LL;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N, -1) {}
int root(int x) {
if (par[x] < 0)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] += par[ry];
par[ry] = rx;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -par[root(x)]; }
};
void solve() {
int N, M, K;
cin >> N >> M >> K;
UnionFind uf(N);
vector<int> deg(N, 0);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
deg[a]++;
deg[b]++;
uf.unite(a, b);
}
vector<vector<int>> block(N);
for (int i = 0; i < K; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
block[a].push_back(b);
block[b].push_back(a);
}
for (int i = 0; i < N; i++) {
int ans = uf.size(i) - deg[i] - 1;
for (int b : block[i]) {
if (uf.same(i, b))
ans--;
}
cout << ans << ' ';
}
cout << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// cout << fixed << setprecision(12);
solve();
return 0;
}
| replace | 22 | 23 | 22 | 23 | TLE | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep2(i, n) for (int i = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define double long double
#define vector2(a, b, c) \
vector<vector<int>> a(b /*縦!w*/, vector<int>(c /*横!w*/))
#define INF 1e10
#define MOD 1e9 + 7
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using Graph = vector<vector<int>>;
struct edge {
int from, to, cost;
};
struct Union {
vector<int> par;
Union(int n) { par = vector<int>(n, -1); }
int find(int x) {
if (par[x] < 0)
return x;
else
return par[x] = find(par[x]);
}
bool same(int a, int b) { return find(a) == find(b); }
int Size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (Size(b) > Size(a))
swap<int>(a, b);
par[a] += par[b];
par[b] = a;
}
int much(int a) {
vector<int> e(a);
rep(i, a) e[i] = find(i);
sort(all(e));
e.erase(unique(all(e)), e.end());
return e.size();
}
};
signed main() {
int a, b, c;
cin >> a >> b >> c;
vector<int> d(b), e(b), f(c), g(c);
rep(i, b) cin >> d[i] >> e[i];
rep(i, c) cin >> f[i] >> g[i];
Union l(a);
vector<int> n(a, 0), m(a, 0);
rep(i, b) {
l.unite(d[i] - 1, e[i] - 1);
n[d[i] - 1]++;
n[e[i] - 1]++;
}
rep(i, c) {
if (l.same(f[i], g[i])) {
m[f[i] - 1]++;
m[g[i] - 1]++;
}
}
rep(i, a) { cout << l.Size(i) - n[i] - 1 - m[i] << ' '; }
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep2(i, n) for (int i = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define double long double
#define vector2(a, b, c) \
vector<vector<int>> a(b /*縦!w*/, vector<int>(c /*横!w*/))
#define INF 1e10
#define MOD 1e9 + 7
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using Graph = vector<vector<int>>;
struct edge {
int from, to, cost;
};
struct Union {
vector<int> par;
Union(int n) { par = vector<int>(n, -1); }
int find(int x) {
if (par[x] < 0)
return x;
else
return par[x] = find(par[x]);
}
bool same(int a, int b) { return find(a) == find(b); }
int Size(int a) { return -par[find(a)]; }
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (Size(b) > Size(a))
swap<int>(a, b);
par[a] += par[b];
par[b] = a;
}
int much(int a) {
vector<int> e(a);
rep(i, a) e[i] = find(i);
sort(all(e));
e.erase(unique(all(e)), e.end());
return e.size();
}
};
signed main() {
int a, b, c;
cin >> a >> b >> c;
vector<int> d(b), e(b), f(c), g(c);
rep(i, b) cin >> d[i] >> e[i];
rep(i, c) cin >> f[i] >> g[i];
Union l(a);
vector<int> n(a, 0), m(a, 0);
rep(i, b) {
l.unite(d[i] - 1, e[i] - 1);
n[d[i] - 1]++;
n[e[i] - 1]++;
}
rep(i, c) {
if (l.same(f[i] - 1, g[i] - 1)) {
m[f[i] - 1]++;
m[g[i] - 1]++;
}
}
rep(i, a) { cout << l.Size(i) - n[i] - 1 - m[i] << ' '; }
} | replace | 75 | 76 | 75 | 76 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
if (a % 2 == 0)
return 0;
else
return 1;
}
int gcd(int a, int b) {
if (a % b == 0) {
return b;
} else {
return (gcd(b, a % b));
}
}
int lcm(int a, int b) {
int x = gcd(a, b);
return (a * b / x);
}
using namespace std;
// now
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int Find(int x) {
if (d[x] < 0)
return x;
return Find(d[x]);
}
bool unite(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y)
return false;
if (d[x] < d[y]) {
swap(x, y);
}
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return Find(x) == Find(y); }
int size(int x) { return (-d[Find(x)]); }
};
int deg[100005];
vector<int> to[100005];
int main() {
int n, m, k;
cin >> n >> m >> k;
UnionFind uf(n);
REP(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
uf.unite(a, b);
}
REP(i, k) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
REP(i, n) {
int ans = uf.size(i) - 1 - deg[i];
for (int x : to[i]) {
if (uf.same(i, x))
ans--;
}
cout << ans << " ";
}
cout << endl;
}
| #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
if (a % 2 == 0)
return 0;
else
return 1;
}
int gcd(int a, int b) {
if (a % b == 0) {
return b;
} else {
return (gcd(b, a % b));
}
}
int lcm(int a, int b) {
int x = gcd(a, b);
return (a * b / x);
}
using namespace std;
// now
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int Find(int x) {
if (d[x] < 0)
return x;
return d[x] = Find(d[x]);
}
bool unite(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y)
return false;
if (d[x] < d[y]) {
swap(x, y);
}
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return Find(x) == Find(y); }
int size(int x) { return (-d[Find(x)]); }
};
int deg[100005];
vector<int> to[100005];
int main() {
int n, m, k;
cin >> n >> m >> k;
UnionFind uf(n);
REP(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
uf.unite(a, b);
}
REP(i, k) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
REP(i, n) {
int ans = uf.size(i) - 1 - deg[i];
for (int x : to[i]) {
if (uf.same(i, x))
ans--;
}
cout << ans << " ";
}
cout << endl;
}
| replace | 49 | 50 | 49 | 50 | TLE | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
class UnionFind {
private:
vector<int> par;
public:
UnionFind(int N) {
par.resize(N);
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { return (x == par[x]) ? x : (par[x] = root(par[x])); }
void unite(int x, int y) { par[root(y)] = root(x); }
bool same(int x, int y) { return (root(x) == root(y)); }
};
int main() {
int N;
cin >> N;
int M;
cin >> M;
int K;
cin >> K;
UnionFind UF(N);
vector<ll> FA(M), FB(M), BA(M), BB(M);
for (int i = 0; i < M; i++) {
int A, B;
cin >> A >> B;
A--;
B--;
UF.unite(A, B);
FA[i] = A, FB[i] = B;
}
for (ll i = 0; i < K; i++) {
int A, B;
cin >> A >> B;
A--;
B--;
BA[i] = A, BB[i] = B;
}
vector<int> CNT(N, 0);
for (ll i = 0; i < N; i++) {
CNT[UF.root(i)]++;
}
vector<int> ans(N);
for (ll i = 0; i < N; i++) {
ans[i] = CNT[UF.root(i)];
ans[i]--; // 除く自分
}
for (int i = 0; i < M; i++) {
if (UF.same(FA[i], FB[i])) {
ans[FA[i]]--;
ans[FB[i]]--;
}
}
for (ll i = 0; i < K; i++) {
if (UF.same(BA[i], BB[i])) {
ans[BA[i]]--;
ans[BB[i]]--;
}
}
cout << ans[0];
for (int i = 1; i < ans.size(); i++) {
cout << ' ' << ans[i];
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
class UnionFind {
private:
vector<int> par;
public:
UnionFind(int N) {
par.resize(N);
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { return (x == par[x]) ? x : (par[x] = root(par[x])); }
void unite(int x, int y) { par[root(y)] = root(x); }
bool same(int x, int y) { return (root(x) == root(y)); }
};
int main() {
int N;
cin >> N;
int M;
cin >> M;
int K;
cin >> K;
UnionFind UF(N);
vector<ll> FA(M), FB(M), BA(K), BB(K);
for (int i = 0; i < M; i++) {
int A, B;
cin >> A >> B;
A--;
B--;
UF.unite(A, B);
FA[i] = A, FB[i] = B;
}
for (ll i = 0; i < K; i++) {
int A, B;
cin >> A >> B;
A--;
B--;
BA[i] = A, BB[i] = B;
}
vector<int> CNT(N, 0);
for (ll i = 0; i < N; i++) {
CNT[UF.root(i)]++;
}
vector<int> ans(N);
for (ll i = 0; i < N; i++) {
ans[i] = CNT[UF.root(i)];
ans[i]--; // 除く自分
}
for (int i = 0; i < M; i++) {
if (UF.same(FA[i], FB[i])) {
ans[FA[i]]--;
ans[FB[i]]--;
}
}
for (ll i = 0; i < K; i++) {
if (UF.same(BA[i], BB[i])) {
ans[BA[i]]--;
ans[BB[i]]--;
}
}
cout << ans[0];
for (int i = 1; i < ans.size(); i++) {
cout << ' ' << ans[i];
}
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
using namespace std;
set<int> f[maxn], b[maxn], ts;
vector<set<int>> path;
bool vis[maxn];
int ans[maxn];
void dfs(int cur) {
if (vis[cur])
return;
vis[cur] = 1;
for (auto &x : f[cur])
dfs(x);
ts.insert(cur);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0, x, y; i < m; ++i) {
cin >> x >> y;
f[x].insert(y);
f[y].insert(x);
}
for (int i = 0, x, y; i < k; ++i) {
cin >> x >> y;
b[x].insert(y);
b[y].insert(x);
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
ts.clear();
dfs(i);
}
int full = ts.size();
for (auto x : ts) {
int value = full - f[x].size();
for (auto v : b[x])
if (ts.count(v))
--value;
ans[x] = value - 1;
}
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " \n"[i == n];
}
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
using namespace std;
set<int> f[maxn], b[maxn], ts;
vector<set<int>> path;
bool vis[maxn];
int ans[maxn];
void dfs(int cur) {
if (vis[cur])
return;
vis[cur] = 1;
for (auto &x : f[cur])
dfs(x);
ts.insert(cur);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0, x, y; i < m; ++i) {
cin >> x >> y;
f[x].insert(y);
f[y].insert(x);
}
for (int i = 0, x, y; i < k; ++i) {
cin >> x >> y;
b[x].insert(y);
b[y].insert(x);
}
for (int i = 1; i <= n; ++i) {
if (vis[i])
continue;
if (!vis[i]) {
ts.clear();
dfs(i);
}
int full = ts.size();
for (auto x : ts) {
int value = full - f[x].size();
for (auto v : b[x])
if (ts.count(v))
--value;
ans[x] = value - 1;
}
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " \n"[i == n];
}
return 0;
} | insert | 49 | 49 | 49 | 51 | TLE | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int id;
vector<int> cc_id;
void dfs(vector<vector<int>> G, int v, int &cnt) {
cc_id[v] = id;
cnt++;
for (auto i : G[v]) {
if (cc_id[i] != -1)
continue;
dfs(G, i, cnt);
}
}
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<vector<int>> G(N);
vector<int> cc_num;
rep(i, M) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
cc_id.assign(N, -1);
id = 0;
rep(i, N) {
if (cc_id[i] == -1) {
int cnt = 0;
dfs(G, i, cnt);
cc_num.push_back(cnt);
id++;
}
}
vector<int> ans(N);
rep(i, N) ans[i] = cc_num[cc_id[i]] - (int)G[i].size() - 1;
rep(i, K) {
int c, d;
cin >> c >> d;
c--;
d--;
if (cc_id[c] == cc_id[d]) {
ans[c]--;
ans[d]--;
}
}
rep(i, N) cout << ans[i] << ' ';
cout << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int id;
vector<int> cc_id;
void dfs(vector<vector<int>> &G, int v, int &cnt) {
cc_id[v] = id;
cnt++;
for (auto i : G[v]) {
if (cc_id[i] != -1)
continue;
dfs(G, i, cnt);
}
}
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<vector<int>> G(N);
vector<int> cc_num;
rep(i, M) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
cc_id.assign(N, -1);
id = 0;
rep(i, N) {
if (cc_id[i] == -1) {
int cnt = 0;
dfs(G, i, cnt);
cc_num.push_back(cnt);
id++;
}
}
vector<int> ans(N);
rep(i, N) ans[i] = cc_num[cc_id[i]] - (int)G[i].size() - 1;
rep(i, K) {
int c, d;
cin >> c >> d;
c--;
d--;
if (cc_id[c] == cc_id[d]) {
ans[c]--;
ans[d]--;
}
}
rep(i, N) cout << ans[i] << ' ';
cout << endl;
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p02762 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define PIE 3.14159265358979323
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> T GCD(T a, T b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }
template <class T> struct Union_find {
/*
T: integer(int, long longなど)
n個の整数に対してunion find森を生成
par: 親の配列 ex)par[x]はxの親
rank: ランクの配列で、root以外は意味はない
size_of: サイズの配列で、root以外は意味がない
root(x): xの根を返す
same(x, y): boolでxとyが連結かを返す
size(x): xが含まれる木の要素数を返す
unite(x, y): xが含まれる木とyが含まれる木を連結させる
*/
vector<int> par;
vector<int> rank;
vector<int> size_of;
Union_find(T n) : par(n), rank(n) {
for (int i = 0; i < n; i++) {
par[i] = i;
rank[i] = 0;
size_of[i] = 1;
}
}
int root(T x) { return par[x] == x ? x : par[x] = root(par[x]); }
bool same(T x, T y) { return root(x) == root(y); }
int size(T x) { return size_of[root(x)]; }
void unite(T x, T y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (rank[x] < rank[y]) {
size_of[y] += size_of[x];
par[x] = y;
} else {
size_of[x] += size_of[y];
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
return;
}
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<set<int>> cannot_make_friends_with(n);
int a, b;
Union_find<int> uf(n);
rep(i, m) {
cin >> a >> b;
a--;
b--;
uf.unite(a, b);
cannot_make_friends_with[a].insert(b);
cannot_make_friends_with[b].insert(a);
}
int c, d;
rep(i, k) {
cin >> c >> d;
c--;
d--;
if (uf.same(c, d)) {
cannot_make_friends_with[c].insert(d);
cannot_make_friends_with[d].insert(c);
}
}
rep(i, n) {
cout << uf.size(i) - cannot_make_friends_with[i].size() - 1;
if (i != n - 1)
cout << " ";
else
cout << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define PIE 3.14159265358979323
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> T GCD(T a, T b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }
template <class T> struct Union_find {
/*
T: integer(int, long longなど)
n個の整数に対してunion find森を生成
par: 親の配列 ex)par[x]はxの親
rank: ランクの配列で、root以外は意味はない
size_of: サイズの配列で、root以外は意味がない
root(x): xの根を返す
same(x, y): boolでxとyが連結かを返す
size(x): xが含まれる木の要素数を返す
unite(x, y): xが含まれる木とyが含まれる木を連結させる
*/
vector<int> par;
vector<int> rank;
vector<int> size_of;
Union_find(T n) : par(n), rank(n) {
for (int i = 0; i < n; i++) {
par[i] = i;
rank[i] = 0;
size_of.push_back(1);
}
}
int root(T x) { return par[x] == x ? x : par[x] = root(par[x]); }
bool same(T x, T y) { return root(x) == root(y); }
int size(T x) { return size_of[root(x)]; }
void unite(T x, T y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (rank[x] < rank[y]) {
size_of[y] += size_of[x];
par[x] = y;
} else {
size_of[x] += size_of[y];
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
return;
}
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<set<int>> cannot_make_friends_with(n);
int a, b;
Union_find<int> uf(n);
rep(i, m) {
cin >> a >> b;
a--;
b--;
uf.unite(a, b);
cannot_make_friends_with[a].insert(b);
cannot_make_friends_with[b].insert(a);
}
int c, d;
rep(i, k) {
cin >> c >> d;
c--;
d--;
if (uf.same(c, d)) {
cannot_make_friends_with[c].insert(d);
cannot_make_friends_with[d].insert(c);
}
}
rep(i, n) {
cout << uf.size(i) - cannot_make_friends_with[i].size() - 1;
if (i != n - 1)
cout << " ";
else
cout << endl;
}
return 0;
} | replace | 63 | 64 | 63 | 64 | -11 | |
p02762 | C++ | Time Limit Exceeded | #include <cstdio>
#include <iostream>
#define N 100005
using namespace std;
int n, m, k, a[N], b[N], pr[N], cp[N], cm[N];
int fnd(int p) { return pr[p] == p ? p : fnd(pr[p]); }
int main() {
int i, x, y;
cin >> n >> m >> k;
for (i = 1; i <= n; i++)
pr[i] = i, cp[i] = 1;
while (m--) {
scanf("%d%d", &x, &y);
cm[x]++, cm[y]++;
x = fnd(x);
y = fnd(y);
if (x != y) {
pr[y] = x;
cp[x] += cp[y];
}
}
while (k--) {
scanf("%d%d", &x, &y);
if (fnd(x) == fnd(y))
cm[x]++, cm[y]++;
}
for (i = 1; i <= n; i++) {
printf("%d ", cp[fnd(i)] - cm[i] - 1);
}
return 0;
} | #include <cstdio>
#include <iostream>
#define N 100005
using namespace std;
int n, m, k, a[N], b[N], pr[N], cp[N], cm[N];
int fnd(int p) { return pr[p] == p ? p : pr[p] = fnd(pr[p]); }
int main() {
int i, x, y;
cin >> n >> m >> k;
for (i = 1; i <= n; i++)
pr[i] = i, cp[i] = 1;
while (m--) {
scanf("%d%d", &x, &y);
cm[x]++, cm[y]++;
x = fnd(x);
y = fnd(y);
if (x != y) {
pr[y] = x;
cp[x] += cp[y];
}
}
while (k--) {
scanf("%d%d", &x, &y);
if (fnd(x) == fnd(y))
cm[x]++, cm[y]++;
}
for (i = 1; i <= n; i++) {
printf("%d ", cp[fnd(i)] - cm[i] - 1);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include<iostream>
using namespace std;
const int N = 1e5 + 5;
int pa[N], cnt[N], fr[N];
int n, m, k;
int search(int x) { return pa[x] == x ? x : search(pa[x]); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
// while(cin>>n>>m>>k)
for (int i = 1; i <= n; i++) {
pa[i] = i;
cnt[i] = 1;
fr[i] = 0;
}
int x, y;
while (m--) {
cin >> x >> y;
int a = search(x);
int b = search(y);
fr[x]++;
fr[y]++;
if (a != b) {
pa[a] = b;
cnt[b] += cnt[a];
}
}
for (int i = 1; i <= n; i++) {
cnt[i] = cnt[search(i)];
}
while (k--) {
cin >> x >> y;
int a = search(x);
int b = search(y);
if (a == b) {
cnt[x]--, cnt[y]--;
}
}
for (int i = 1; i <= n; i++)
cout << cnt[i] - fr[i] - 1 << ' ';
cout << endl;
return 0;
} | #include <bits/stdc++.h>
// #include<iostream>
using namespace std;
const int N = 1e5 + 5;
int pa[N], cnt[N], fr[N];
int n, m, k;
int search(int x) {
// return pa[x]==pa[pa[x]]?pa[x]:pa[x]=search(pa[x]);
if (pa[x] != pa[pa[x]])
pa[x] = search(pa[x]);
return pa[x];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
// while(cin>>n>>m>>k)
for (int i = 1; i <= n; i++) {
pa[i] = i;
cnt[i] = 1;
fr[i] = 0;
}
int x, y;
while (m--) {
cin >> x >> y;
int a = search(x);
int b = search(y);
fr[x]++;
fr[y]++;
if (a != b) {
pa[a] = b;
cnt[b] += cnt[a];
}
}
for (int i = 1; i <= n; i++) {
cnt[i] = cnt[search(i)];
}
while (k--) {
cin >> x >> y;
int a = search(x);
int b = search(y);
if (a == b) {
cnt[x]--, cnt[y]--;
}
}
for (int i = 1; i <= n; i++)
cout << cnt[i] - fr[i] - 1 << ' ';
cout << endl;
return 0;
} | replace | 6 | 7 | 6 | 12 | TLE | |
p02762 | Python | Runtime Error | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import defaultdict
def main():
n, m, k = map(int, input().split())
fr_ed_c = defaultdict(int) # node -> count
parents = {} # node -> parent_node
root_2_nc = {} # node -> count
for i in range(m):
a, b = map(int, input().split())
fr_ed_c[a] += 1
fr_ed_c[b] += 1
p_a = get_root(parents, a)
p_b = get_root(parents, b)
if p_a == p_b:
# already same cluster. do nothing.
pass
else:
# cluster integration
p_a_c = root_2_nc.get(p_a, 1)
p_b_c = root_2_nc.get(p_b, 1)
p_a, p_b = (p_a, p_b) if p_a_c < p_b_c else (p_b, p_a)
parents[p_a] = p_b
if p_b not in root_2_nc:
root_2_nc[p_b] = 1
root_2_nc[p_b] += root_2_nc.pop(p_a, 1)
br_ed = defaultdict(set) # node -> set()
for i in range(k):
c, d = map(int, input().split())
br_ed[c].add(d)
br_ed[d].add(c)
ans_list = []
for i_m in range(n):
i = i_m + 1
root = get_root(parents, i)
block_count = 0
for b in br_ed[i]:
b_root = get_root(parents, b)
block_count += 1 if root == b_root else 0
ans = root_2_nc[root] - 1 - fr_ed_c[i] - block_count
ans_list.append(str(ans))
print(" ".join(ans_list))
def get_root(parents, i):
if i not in parents:
return i
return get_root(parents, parents[i])
if __name__ == "__main__":
main()
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import defaultdict
def main():
n, m, k = map(int, input().split())
fr_ed_c = defaultdict(int) # node -> count
parents = {} # node -> parent_node
root_2_nc = {} # node -> count
for i in range(m):
a, b = map(int, input().split())
fr_ed_c[a] += 1
fr_ed_c[b] += 1
p_a = get_root(parents, a)
p_b = get_root(parents, b)
if p_a == p_b:
# already same cluster. do nothing.
pass
else:
# cluster integration
p_a_c = root_2_nc.get(p_a, 1)
p_b_c = root_2_nc.get(p_b, 1)
p_a, p_b = (p_a, p_b) if p_a_c < p_b_c else (p_b, p_a)
parents[p_a] = p_b
if p_b not in root_2_nc:
root_2_nc[p_b] = 1
root_2_nc[p_b] += root_2_nc.pop(p_a, 1)
br_ed = defaultdict(set) # node -> set()
for i in range(k):
c, d = map(int, input().split())
br_ed[c].add(d)
br_ed[d].add(c)
ans_list = []
for i_m in range(n):
i = i_m + 1
root = get_root(parents, i)
block_count = 0
for b in br_ed[i]:
b_root = get_root(parents, b)
block_count += 1 if root == b_root else 0
ans = root_2_nc.get(root, 1) - 1 - fr_ed_c.get(i, 0) - block_count
ans_list.append(str(ans))
print(" ".join(ans_list))
def get_root(parents, i):
if i not in parents:
return i
return get_root(parents, parents[i])
if __name__ == "__main__":
main()
| replace | 49 | 50 | 49 | 50 | 0 | |
p02762 | Python | Runtime Error | class UnionFind:
def __init__(self, n_nodes):
self.parent = [i for i in range(n_nodes)]
self.rank = [0] * n_nodes
self.size = [1] * n_nodes
def find(self, x):
if x == self.parent[x]:
return x
else:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.rank[x] > self.rank[y]:
self.parent[y] = x
self.size[x] += self.size[y]
else:
self.parent[x] = y
self.size[y] += self.size[x]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def check(self, x, y):
return self.find(x) == self.find(y)
def get_size(self, x):
return self.size[self.find(x)]
N, M, K = map(int, input().split())
tree = UnionFind(N)
exclusion = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
tree.unite(a, b)
exclusion[a].append(b)
exclusion[b].append(a)
for _ in range(K):
c, d = map(int, input().split())
c -= 1
d -= 1
if not tree.check(c, d):
continue
exclusion[c].append(d)
exclusion[d].append(c)
for i in range(N):
# 同一グループで自分以外の人数
n = tree.get_size(i) - 1
ans = n - len(exclusion[i])
print(ans, end=" ")
| import sys
sys.setrecursionlimit(10**6)
class UnionFind:
def __init__(self, n_nodes):
self.parent = [i for i in range(n_nodes)]
self.rank = [0] * n_nodes
self.size = [1] * n_nodes
def find(self, x):
if x == self.parent[x]:
return x
else:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.rank[x] > self.rank[y]:
self.parent[y] = x
self.size[x] += self.size[y]
else:
self.parent[x] = y
self.size[y] += self.size[x]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def check(self, x, y):
return self.find(x) == self.find(y)
def get_size(self, x):
return self.size[self.find(x)]
N, M, K = map(int, input().split())
tree = UnionFind(N)
exclusion = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
tree.unite(a, b)
exclusion[a].append(b)
exclusion[b].append(a)
for _ in range(K):
c, d = map(int, input().split())
c -= 1
d -= 1
if not tree.check(c, d):
continue
exclusion[c].append(d)
exclusion[d].append(c)
for i in range(N):
# 同一グループで自分以外の人数
n = tree.get_size(i) - 1
ans = n - len(exclusion[i])
print(ans, end=" ")
| insert | 0 | 0 | 0 | 5 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M, K;
cin >> N >> M >> K;
int A[M], B[M];
int C[K], D[K];
for (int i = 0; i < M; ++i) {
cin >> A[i] >> B[i];
--A[i];
--B[i];
}
for (int i = 0; i < K; ++i) {
cin >> C[i] >> D[i];
--C[i];
--D[i];
}
UnionFind uf(N);
for (int i = 0; i < M; ++i) {
uf.unite(A[i], B[i]);
}
int ans[N];
fill(ans, ans + N, 0);
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
if (uf.same(i, j)) {
++ans[i];
++ans[j];
}
}
}
for (int i = 0; i < M; ++i) {
if (uf.same(A[i], B[i])) {
--ans[A[i]];
--ans[B[i]];
}
}
for (int i = 0; i < K; ++i) {
if (uf.same(C[i], D[i])) {
--ans[C[i]];
--ans[D[i]];
}
}
for (int i = 0; i < N; ++i) {
if (i != 0) {
cout << " ";
}
cout << ans[i];
}
cout << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry)
return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M, K;
cin >> N >> M >> K;
int A[M], B[M];
int C[K], D[K];
for (int i = 0; i < M; ++i) {
cin >> A[i] >> B[i];
--A[i];
--B[i];
}
for (int i = 0; i < K; ++i) {
cin >> C[i] >> D[i];
--C[i];
--D[i];
}
UnionFind uf(N);
for (int i = 0; i < M; ++i) {
uf.unite(A[i], B[i]);
}
int ans[N];
fill(ans, ans + N, 0);
{
int t[N];
fill(t, t + N, 0);
for (int i = 0; i < N; ++i) {
++t[uf.root(i)];
}
for (int i = 0; i < N; ++i) {
ans[i] = t[uf.root(i)] - 1;
}
}
for (int i = 0; i < M; ++i) {
if (uf.same(A[i], B[i])) {
--ans[A[i]];
--ans[B[i]];
}
}
for (int i = 0; i < K; ++i) {
if (uf.same(C[i], D[i])) {
--ans[C[i]];
--ans[D[i]];
}
}
for (int i = 0; i < N; ++i) {
if (i != 0) {
cout << " ";
}
cout << ans[i];
}
cout << endl;
return 0;
}
| replace | 61 | 67 | 61 | 70 | TLE | |
p02762 | C++ | Runtime Error | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50010
struct uftree {
int cnt;
int rank;
int parent;
} set[MAX];
int fr[100000 + 5];
void make_set(int n) {
int i;
for (i = 0; i <= n; i++) {
set[i].cnt = 1;
set[i].rank = 0;
set[i].parent = i;
}
}
int set_find(int x) {
if (set[x].parent == x)
return x;
else
return set[x].parent = set_find(set[x].parent);
}
void set_join(int x, int y) {
int a = set_find(x);
int b = set_find(y);
if (set[a].rank > set[b].rank) {
set[b].parent = a;
if (a != b)
set[a].cnt += set[b].cnt;
} else {
set[a].parent = b;
if (a != b)
set[b].cnt += set[a].cnt;
if (set[a].rank == set[b].rank) {
set[b].rank++;
}
}
}
int cmp(const void *a, const void *b) {
uftree *p1 = (uftree *)a, *p2 = (uftree *)b;
return p2->cnt - p1->cnt;
}
int query(int n) { return set[set_find(n)].cnt; }
int t, n, m, i, a, b, ans, k;
int main() {
scanf("%d %d %d", &n, &m, &k);
make_set(n);
for (i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
fr[a]++;
fr[b]++;
set_join(a, b);
}
for (int i = 1; i <= n; i++)
fr[i] = query(i) - fr[i] - 1;
for (int i = 0; i < k; i++) {
scanf("%d%d", &a, &b);
if (set_find(a) == set_find(b))
fr[a]--, fr[b]--;
}
for (int i = 1; i <= n; i++) {
if (i != 1)
printf(" ");
printf("%d", fr[i]);
}
printf("\n");
return 0;
}
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100010
struct uftree {
int cnt;
int rank;
int parent;
} set[MAX];
int fr[100000 + 5];
void make_set(int n) {
int i;
for (i = 0; i <= n; i++) {
set[i].cnt = 1;
set[i].rank = 0;
set[i].parent = i;
}
}
int set_find(int x) {
if (set[x].parent == x)
return x;
else
return set[x].parent = set_find(set[x].parent);
}
void set_join(int x, int y) {
int a = set_find(x);
int b = set_find(y);
if (set[a].rank > set[b].rank) {
set[b].parent = a;
if (a != b)
set[a].cnt += set[b].cnt;
} else {
set[a].parent = b;
if (a != b)
set[b].cnt += set[a].cnt;
if (set[a].rank == set[b].rank) {
set[b].rank++;
}
}
}
int cmp(const void *a, const void *b) {
uftree *p1 = (uftree *)a, *p2 = (uftree *)b;
return p2->cnt - p1->cnt;
}
int query(int n) { return set[set_find(n)].cnt; }
int t, n, m, i, a, b, ans, k;
int main() {
scanf("%d %d %d", &n, &m, &k);
make_set(n);
for (i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
fr[a]++;
fr[b]++;
set_join(a, b);
}
for (int i = 1; i <= n; i++)
fr[i] = query(i) - fr[i] - 1;
for (int i = 0; i < k; i++) {
scanf("%d%d", &a, &b);
if (set_find(a) == set_find(b))
fr[a]--, fr[b]--;
}
for (int i = 1; i <= n; i++) {
if (i != 1)
printf(" ");
printf("%d", fr[i]);
}
printf("\n");
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02762 | C++ | Runtime Error | /*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
* * WRITER:kakitamasziru/OxOmisosiru * *
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG // FOR THE DEBUG! COMMENT OUT THIS WHEN SUBMITTING!
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint> // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits> //setprecision
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <utility> // pair, make_pair
#include <vector> // vector
// #include <cstdio> // printf
#include <bitset> // bitset
#include <cmath> //abs,,,
#include <deque> // deque
#include <map> // map
#include <math.h> //pow,,,
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
// It is so troublesome that I include bits/stdc++.h !
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const long long INF = 1001001001001001;
const long long MOD = 1000000007;
typedef pair<long long, long long> P;
// NのM乗を求める(繰り返し二乗法)
long long mod_pow(long long N, long long M) {
if (M == 0)
return 1;
long long res = mod_pow((N * N) % MOD, M / 2);
// 最下位ビット(*N)が1の時は単独でNをかける
if (M & 1)
res = (res * N) % MOD;
return res %= MOD;
}
long long nCr(long long n, long long r) {
long long A = 1, B = 1;
// Aが分子Bが1/r!
for (long long i = n - r + 1; i <= n; i++) {
A = A * i % MOD;
}
for (long long i = 1; i <= r; i++) {
B = B * i % MOD;
}
B = mod_pow(B, MOD - 2);
B %= MOD;
// Bは割るのではなく掛ける
return (A * B) % MOD;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long A, B;
bool ok(long long key, long long index) {
string S = to_string(index);
long long nagasa = S.size();
if (index * A + nagasa * B > key)
return true;
else if (index * A + nagasa * B <= key)
return false;
}
long long binary_search(long long key, long long size) {
// left,right,midはいずれもindex
long long left = -1, right = size;
while (right - left > 1) {
long long mid = left + (right - left) / 2;
long long hantei = ok(key, mid);
if (hantei)
right = mid;
else
left = mid;
}
if (left == -1)
return 0;
return left;
}
int parent[11451];
int connectSize[11451];
int tall[11451];
int searchRoot(int V) {
// 頂点Vの親==頂点Vということは頂点Vはその木の根である。
if (parent[V] == V) {
return V;
} else {
// 一回ごとにparent[V]を更新する。辺の縮約
return parent[V] = searchRoot(parent[V]);
}
}
bool same(int X, int Y) {
if (searchRoot(X) == searchRoot(Y))
return true;
else
return false;
}
void unite(int X, int Y) {
// X,Yの高さが高い方の木の「高さ0の根」に高さが低い方の根を付ける
int Xroot = searchRoot(X);
int Yroot = searchRoot(Y);
// 同じ根を持つ木を併合することはしなくてよい
if (Xroot == Yroot)
return;
if (connectSize[Xroot] > connectSize[Yroot]) {
parent[Yroot] = Xroot;
connectSize[Xroot] += connectSize[Yroot];
// connectSize[Xroot] = connectSize[Yroot];
} else if (connectSize[Xroot] == connectSize[Yroot]) {
parent[Yroot] = Xroot;
// 高さが同じとき、YがXの根の下に入るのでYの深さはXより1深くなる
// connectSize[Yroot]++;
connectSize[Xroot] += connectSize[Yroot];
// connectSize[X] = connectSize[Y];
} else {
parent[Xroot] = Yroot;
connectSize[Yroot] += connectSize[Xroot];
// connectSize[Xroot] = connectSize[Yroot];
}
}
vector<vector<int>> Fri(114514);
vector<vector<int>> Blo(114514);
int main() {
int N, M, K;
cin >> N >> M >> K;
for (int i = 0; i < 114514; i++) {
parent[i] = i;
connectSize[i] = 1;
tall[i] = 0;
}
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
unite(a, b);
Fri.at(a).push_back(b);
Fri.at(b).push_back(a);
}
for (int i = 0; i < K; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
Blo.at(c).push_back(d);
Blo.at(d).push_back(c);
}
int ans = 0;
for (int i = 0; i < N; i++) {
ans = connectSize[searchRoot(i)] - Fri.at(i).size() - 1;
// cout << connectSize[i] << " " << Fri.at(i).size() << " ";
for (int j = 0; j < Blo.at(i).size(); j++) {
if (same(i, Blo.at(i).at(j)))
ans--;
}
cout << ans << endl;
}
// PLEASE GIVE ME THE "ACCEPTED" !
}
| /*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
* * WRITER:kakitamasziru/OxOmisosiru * *
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG // FOR THE DEBUG! COMMENT OUT THIS WHEN SUBMITTING!
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint> // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits> //setprecision
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <utility> // pair, make_pair
#include <vector> // vector
// #include <cstdio> // printf
#include <bitset> // bitset
#include <cmath> //abs,,,
#include <deque> // deque
#include <map> // map
#include <math.h> //pow,,,
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
// It is so troublesome that I include bits/stdc++.h !
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const long long INF = 1001001001001001;
const long long MOD = 1000000007;
typedef pair<long long, long long> P;
// NのM乗を求める(繰り返し二乗法)
long long mod_pow(long long N, long long M) {
if (M == 0)
return 1;
long long res = mod_pow((N * N) % MOD, M / 2);
// 最下位ビット(*N)が1の時は単独でNをかける
if (M & 1)
res = (res * N) % MOD;
return res %= MOD;
}
long long nCr(long long n, long long r) {
long long A = 1, B = 1;
// Aが分子Bが1/r!
for (long long i = n - r + 1; i <= n; i++) {
A = A * i % MOD;
}
for (long long i = 1; i <= r; i++) {
B = B * i % MOD;
}
B = mod_pow(B, MOD - 2);
B %= MOD;
// Bは割るのではなく掛ける
return (A * B) % MOD;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long A, B;
bool ok(long long key, long long index) {
string S = to_string(index);
long long nagasa = S.size();
if (index * A + nagasa * B > key)
return true;
else if (index * A + nagasa * B <= key)
return false;
}
long long binary_search(long long key, long long size) {
// left,right,midはいずれもindex
long long left = -1, right = size;
while (right - left > 1) {
long long mid = left + (right - left) / 2;
long long hantei = ok(key, mid);
if (hantei)
right = mid;
else
left = mid;
}
if (left == -1)
return 0;
return left;
}
int parent[114514];
int connectSize[114514];
int tall[114514];
int searchRoot(int V) {
// 頂点Vの親==頂点Vということは頂点Vはその木の根である。
if (parent[V] == V) {
return V;
} else {
// 一回ごとにparent[V]を更新する。辺の縮約
return parent[V] = searchRoot(parent[V]);
}
}
bool same(int X, int Y) {
if (searchRoot(X) == searchRoot(Y))
return true;
else
return false;
}
void unite(int X, int Y) {
// X,Yの高さが高い方の木の「高さ0の根」に高さが低い方の根を付ける
int Xroot = searchRoot(X);
int Yroot = searchRoot(Y);
// 同じ根を持つ木を併合することはしなくてよい
if (Xroot == Yroot)
return;
if (connectSize[Xroot] > connectSize[Yroot]) {
parent[Yroot] = Xroot;
connectSize[Xroot] += connectSize[Yroot];
// connectSize[Xroot] = connectSize[Yroot];
} else if (connectSize[Xroot] == connectSize[Yroot]) {
parent[Yroot] = Xroot;
// 高さが同じとき、YがXの根の下に入るのでYの深さはXより1深くなる
// connectSize[Yroot]++;
connectSize[Xroot] += connectSize[Yroot];
// connectSize[X] = connectSize[Y];
} else {
parent[Xroot] = Yroot;
connectSize[Yroot] += connectSize[Xroot];
// connectSize[Xroot] = connectSize[Yroot];
}
}
vector<vector<int>> Fri(114514);
vector<vector<int>> Blo(114514);
int main() {
int N, M, K;
cin >> N >> M >> K;
for (int i = 0; i < 114514; i++) {
parent[i] = i;
connectSize[i] = 1;
tall[i] = 0;
}
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
unite(a, b);
Fri.at(a).push_back(b);
Fri.at(b).push_back(a);
}
for (int i = 0; i < K; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
Blo.at(c).push_back(d);
Blo.at(d).push_back(c);
}
int ans = 0;
for (int i = 0; i < N; i++) {
ans = connectSize[searchRoot(i)] - Fri.at(i).size() - 1;
// cout << connectSize[i] << " " << Fri.at(i).size() << " ";
for (int j = 0; j < Blo.at(i).size(); j++) {
if (same(i, Blo.at(i).at(j)))
ans--;
}
cout << ans << endl;
}
// PLEASE GIVE ME THE "ACCEPTED" !
}
| replace | 104 | 107 | 104 | 107 | -11 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int, int>
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;
class UnionFind {
// 各ノードが根である時の高さ
// ノードが根でないときは意味をなさない
vector<int> rank;
// 各ノードが属する木の根
// findSetするときのみ更新される
vector<int> p;
// 二つの根を結合
void link(int rx, int ry) {
if (rx == ry)
return;
if (rank[rx] > rank[ry])
p[ry] = rx;
else
p[rx] = ry;
if (rank[rx] == rank[ry])
rank[ry]++;
}
public:
// 根を作成
// その木の高さは0
void makeSet(int x) {
p[x] = x;
rank[x] = 0;
}
// 初期化
// 入力サイズ分のノードを確保
// それらのノードをそれぞれ根とする木を作成
// ノードの値は 0 ~ size-1
UnionFind(int size) {
rank.resize(size, 0);
p.resize(size, 0);
rep(i, size) makeSet(i);
}
// あるノードの属する木の根を返す
// 根を探す過程で途中のノードの親も更新
int findSet(int x) {
if (x != p[x])
p[x] = findSet(p[x]);
return p[x];
}
// 二つのノードが同じ木にあるか判定
bool same(int x, int y) { return findSet(x) == findSet(y); }
// 二つのノードの属する木を結合
void unite(int x, int y) { link(findSet(x), findSet(y)); }
};
int main() {
fast_io int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> fr(n), bl(n);
int a, b;
UnionFind tree(n);
rep(i, m) {
cin >> a >> b;
a--;
b--;
fr[a].push_back(b);
fr[b].push_back(a);
tree.unite(a, b);
}
rep(i, k) {
cin >> a >> b;
a--;
b--;
bl[a].push_back(b);
bl[b].push_back(a);
}
map<int, int> mp;
rep(i, n) mp[tree.findSet(i + 1)]++;
int ans;
rep(i, n) {
ans = mp[tree.findSet(i)] - 1 - fr[i].size();
for (int block : bl[i])
if (tree.same(i, block))
ans--;
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int, int>
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;
class UnionFind {
// 各ノードが根である時の高さ
// ノードが根でないときは意味をなさない
vector<int> rank;
// 各ノードが属する木の根
// findSetするときのみ更新される
vector<int> p;
// 二つの根を結合
void link(int rx, int ry) {
if (rx == ry)
return;
if (rank[rx] > rank[ry])
p[ry] = rx;
else
p[rx] = ry;
if (rank[rx] == rank[ry])
rank[ry]++;
}
public:
// 根を作成
// その木の高さは0
void makeSet(int x) {
p[x] = x;
rank[x] = 0;
}
// 初期化
// 入力サイズ分のノードを確保
// それらのノードをそれぞれ根とする木を作成
// ノードの値は 0 ~ size-1
UnionFind(int size) {
rank.resize(size, 0);
p.resize(size, 0);
rep(i, size) makeSet(i);
}
// あるノードの属する木の根を返す
// 根を探す過程で途中のノードの親も更新
int findSet(int x) {
if (x != p[x])
p[x] = findSet(p[x]);
return p[x];
}
// 二つのノードが同じ木にあるか判定
bool same(int x, int y) { return findSet(x) == findSet(y); }
// 二つのノードの属する木を結合
void unite(int x, int y) { link(findSet(x), findSet(y)); }
};
int main() {
fast_io int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> fr(n), bl(n);
int a, b;
UnionFind tree(n);
rep(i, m) {
cin >> a >> b;
a--;
b--;
fr[a].push_back(b);
fr[b].push_back(a);
tree.unite(a, b);
}
rep(i, k) {
cin >> a >> b;
a--;
b--;
bl[a].push_back(b);
bl[b].push_back(a);
}
map<int, int> mp;
rep(i, n) mp[tree.findSet(i)]++;
int ans;
rep(i, n) {
ans = mp[tree.findSet(i)] - 1 - fr[i].size();
for (int block : bl[i])
if (tree.same(i, block))
ans--;
cout << ans << endl;
}
return 0;
} | replace | 83 | 84 | 83 | 84 | 0 | |
p02762 | C++ | Runtime Error | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;
class UnionFind {
public:
vector<int> d;
UnionFind(int n) : d(n, -1) {}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return false; // xとyの根が同じ(=同じ木にある)時はそのまま
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) { return -d[root(x)]; }
};
signed _main() {
int N, M, K;
cin >> N >> M >> K;
v<int> deg(N, 0);
UnionFind Friend(N);
v<int> block(N, 0);
rep(i, 0, M) {
int a, b;
cin >> a >> b;
a--;
b--;
Friend.unite(a, b);
deg[a]++;
deg[b]++;
}
rep(i, 0, K) {
int a, b;
cin >> a >> b;
a--;
b--;
if (Friend.same(a, b)) {
block[a]++;
block[b]++;
}
}
rep(i, 0, N) { cout << Friend.size(i) - deg[i] - 1 - block[i] << endl; }
return 0;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, m, n) for (int i = (m); i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;
class UnionFind {
public:
vector<int> d;
UnionFind(int n) : d(n, -1) {}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) { // xとyの木を併合
x = root(x); // xの根をrx
y = root(y); // yの根をry
if (x == y)
return false; // xとyの根が同じ(=同じ木にある)時はそのまま
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) { return -d[root(x)]; }
};
signed _main() {
int N, M, K;
cin >> N >> M >> K;
v<int> deg(N, 0);
UnionFind Friend(N);
v<int> block(N, 0);
rep(i, 0, M) {
int a, b;
cin >> a >> b;
a--;
b--;
Friend.unite(a, b);
deg[a]++;
deg[b]++;
}
rep(i, 0, K) {
int a, b;
cin >> a >> b;
a--;
b--;
if (Friend.same(a, b)) {
block[a]++;
block[b]++;
}
}
rep(i, 0, N) { cout << Friend.size(i) - deg[i] - 1 - block[i] << endl; }
return 0;
}
| replace | 48 | 51 | 48 | 51 | -11 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
const double PI = 3.14159265358979323846;
int N, M, K;
vector<vector<int>> graph;
vector<int> group;
vector<int> group_count;
vector<vector<int>> block;
int main() {
cin >> N >> M >> K;
graph.resize(N);
block.resize(N);
group.resize(N);
group_count.resize(N);
rp(i, 0, M) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
graph.at(a).emplace_back(b);
graph.at(b).emplace_back(a);
}
rp(i, 0, K) {
int c, d;
scanf("%d%d", &c, &d);
c--;
d--;
block.at(c).emplace_back(d);
block.at(d).emplace_back(c);
}
int g = 1;
rp(i, 0, N) {
if (group.at(i) == 0) {
queue<int> q;
q.push(i);
while (!q.empty()) {
int s = q.front();
q.pop();
if (group.at(s) == 0) {
group.at(s) = g;
group_count.at(g)++;
for (auto x : graph.at(s)) {
if (group[x] == 0)
q.push(x);
}
}
}
g++;
}
}
rp(i, 0, N) {
int res = group_count[group[i]] - 1 - int(graph[i].size());
for (auto x : block[i]) {
if (group[i] == group[x])
res--;
}
printf("%d ", res);
}
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
const double PI = 3.14159265358979323846;
int N, M, K;
vector<vector<int>> graph;
vector<int> group;
vector<int> group_count;
vector<vector<int>> block;
int main() {
cin >> N >> M >> K;
graph.resize(N);
block.resize(N);
group.resize(N);
group_count.resize(N + 10);
rp(i, 0, M) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
graph.at(a).emplace_back(b);
graph.at(b).emplace_back(a);
}
rp(i, 0, K) {
int c, d;
scanf("%d%d", &c, &d);
c--;
d--;
block.at(c).emplace_back(d);
block.at(d).emplace_back(c);
}
int g = 1;
rp(i, 0, N) {
if (group.at(i) == 0) {
queue<int> q;
q.push(i);
while (!q.empty()) {
int s = q.front();
q.pop();
if (group.at(s) == 0) {
group.at(s) = g;
group_count.at(g)++;
for (auto x : graph.at(s)) {
if (group[x] == 0)
q.push(x);
}
}
}
g++;
}
}
rp(i, 0, N) {
int res = group_count[group[i]] - 1 - int(graph[i].size());
for (auto x : block[i]) {
if (group[i] == group[x])
res--;
}
printf("%d ", res);
}
cout << endl;
return 0;
} | replace | 36 | 37 | 36 | 37 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
void dfs(int &m, int p, int t, vector<vector<int>> frd, vector<bool> &ald,
vector<int> &group) {
if (ald[p] == true) {
return;
}
ald[p] = true;
m++;
group[p] = t;
for (int x : frd[p]) {
dfs(m, x, t, frd, ald, group);
}
return;
} // DFS
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<vector<int>> frd(N + 1, vector<int>(0));
vector<vector<int>> block(N + 1, vector<int>(0));
for (int i = 0; i < M; i++) {
int k, l;
cin >> k >> l;
frd[k].emplace_back(l);
frd[l].emplace_back(k);
}
for (int i = 0; i < K; i++) {
int k, l;
cin >> k >> l;
block[k].emplace_back(l);
block[l].emplace_back(k);
} // 友達関係、ブロック関係の配列取得
vector<bool> ald_f(N + 1, false); // DFSしたかどうか
vector<int> group(N + 1);
vector<int> c(N); // 何回目のDFSで到達したか
int t = 0;
for (int i = 1; i < N + 1; i++) {
if (ald_f[i] == false) {
int m = 0;
dfs(m, i, t, frd, ald_f, group);
c[t] = m;
t++;
}
} // DFSを行いt個にグルーピング
for (int i = 1; i < N + 1; i++) {
int max = c[group[i]];
int h = group[i];
int minus = frd[i].size();
for (int x : block[i]) {
if (group[x] == h) {
minus++;
}
}
cout << max - minus - 1 << " ";
}
// 所属するグループの要素数ー直接の友達ー同じグループでブロック関係の人数-1(自分)
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
void dfs(int &m, int p, int t, const vector<vector<int>> &frd,
vector<bool> &ald, vector<int> &group) {
if (ald[p] == true) {
return;
}
ald[p] = true;
m++;
group[p] = t;
for (int x : frd[p]) {
dfs(m, x, t, frd, ald, group);
}
return;
} // DFS
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<vector<int>> frd(N + 1, vector<int>(0));
vector<vector<int>> block(N + 1, vector<int>(0));
for (int i = 0; i < M; i++) {
int k, l;
cin >> k >> l;
frd[k].emplace_back(l);
frd[l].emplace_back(k);
}
for (int i = 0; i < K; i++) {
int k, l;
cin >> k >> l;
block[k].emplace_back(l);
block[l].emplace_back(k);
} // 友達関係、ブロック関係の配列取得
vector<bool> ald_f(N + 1, false); // DFSしたかどうか
vector<int> group(N + 1);
vector<int> c(N); // 何回目のDFSで到達したか
int t = 0;
for (int i = 1; i < N + 1; i++) {
if (ald_f[i] == false) {
int m = 0;
dfs(m, i, t, frd, ald_f, group);
c[t] = m;
t++;
}
} // DFSを行いt個にグルーピング
for (int i = 1; i < N + 1; i++) {
int max = c[group[i]];
int h = group[i];
int minus = frd[i].size();
for (int x : block[i]) {
if (group[x] == h) {
minus++;
}
}
cout << max - minus - 1 << " ";
}
// 所属するグループの要素数ー直接の友達ー同じグループでブロック関係の人数-1(自分)
cout << endl;
}
| replace | 3 | 5 | 3 | 5 | TLE | |
p02762 | Python | Runtime Error | import sys
sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7
def input():
return sys.stdin.readline().strip()
class UnionFind:
def __init__(self, n_nodes):
self.n_nodes = n_nodes
# self.parents[x] < 0 の時,xが根である.
# また,xが根の時,(-1) * (同一グループの要素数) が格納されるようになる.
self.parents = [-1] * n_nodes
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 unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
# 常にxの方が要素数が多くなるように,スワップする
if self.parents[x] > self.parents[y]:
x, y = y, x
# 要素数の少ない方のグループを,要素数が多い方の木に貼る.
self.parents[x] += self.parents[y]
self.parents[y] = x
def get_size(self, x):
return -self.parents[self.find(x)]
def is_same(self, x, y):
return self.find(x) == self.find(y)
def get_members(self, x):
parent = self.find(x)
return [i for i in range(self.n_nodes) if self.find(i) == parent]
def get_parent_list(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def get_n_groups(self):
return len(self.get_parent_list())
def get_members_dict(self):
return {par: self.get_members(par) for par in self.get_parent_list()}
def main():
N, M, K = map(int, input().split())
tree = UnionFind(N)
friends = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
tree.unite(a, b)
friends[a].append(b)
friends[b].append(a)
ng = [[] for _ in range(N)]
for _ in range(K):
c, d = map(int, input().split())
c -= 1
d -= 1
ng[c].append(d)
ng[d].append(a)
ans = []
for i in range(N):
n_ng = 0
for j in ng[i]:
if tree.is_same(i, j):
n_ng += 1
n_member = tree.get_size(i)
n_friends = len(friends[i])
# 自分を引くのを忘れない
ans.append(n_member - n_friends - n_ng - 1)
print(*ans, sep=" ")
if __name__ == "__main__":
main()
| import sys
sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7
def input():
return sys.stdin.readline().strip()
class UnionFind:
def __init__(self, n_nodes):
self.n_nodes = n_nodes
# self.parents[x] < 0 の時,xが根である.
# また,xが根の時,(-1) * (同一グループの要素数) が格納されるようになる.
self.parents = [-1] * n_nodes
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 unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
# 常にxの方が要素数が多くなるように,スワップする
if self.parents[x] > self.parents[y]:
x, y = y, x
# 要素数の少ない方のグループを,要素数が多い方の木に貼る.
self.parents[x] += self.parents[y]
self.parents[y] = x
def get_size(self, x):
return -self.parents[self.find(x)]
def is_same(self, x, y):
return self.find(x) == self.find(y)
def get_members(self, x):
parent = self.find(x)
return [i for i in range(self.n_nodes) if self.find(i) == parent]
def get_parent_list(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def get_n_groups(self):
return len(self.get_parent_list())
def get_members_dict(self):
return {par: self.get_members(par) for par in self.get_parent_list()}
def main():
N, M, K = map(int, input().split())
tree = UnionFind(N)
friends = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
tree.unite(a, b)
friends[a].append(b)
friends[b].append(a)
ng = [[] for _ in range(N)]
for _ in range(K):
c, d = map(int, input().split())
c -= 1
d -= 1
ng[c].append(d)
ng[d].append(c)
ans = []
for i in range(N):
n_ng = 0
for j in ng[i]:
if tree.is_same(i, j):
n_ng += 1
n_member = tree.get_size(i)
n_friends = len(friends[i])
# 自分を引くのを忘れない
ans.append(n_member - n_friends - n_ng - 1)
print(*ans, sep=" ")
if __name__ == "__main__":
main()
| replace | 80 | 81 | 80 | 81 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// #define int ll
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<bool>> vvb;
typedef map<int, bool> mib;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n - 1); i >= 0; --i)
#define FORE(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << '\n'
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << '\n'
#define debug3(x, y, z) \
cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " \
<< z << '\n'
#define hyphen() cerr << "--\n"
#define ALL(vec) (vec).begin(), (vec).end()
#define REVALL(vec) (vec).rbegin(), (vec).rend()
static const int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
#define fst first
#define snd second
#define pb push_back
#define mk(x, y) make_pair((x), (y))
const int MOD = (int)1e9 + 7;
const int INF = numeric_limits<int>::max();
const int MAX_N = 100000 + 10;
vector<int> par(MAX_N);
vector<int> mysize(MAX_N);
vector<int> myrank(MAX_N);
void init(int n) {
REP(i, n) {
par.at(i) = i;
mysize.at(i) = 1;
myrank.at(i) = 0;
}
}
int root(int x) { return par.at(x) == x ? x : par.at(x) = root(par.at(x)); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (myrank.at(x) < myrank.at(y)) {
par.at(x) = y;
mysize.at(y) += mysize.at(x);
} else {
par.at(y) = x;
mysize.at(x) += mysize.at(y);
if (myrank.at(x) == myrank.at(y))
myrank.at(x)++;
}
}
int size(int x) { return mysize.at(root(x)); }
signed main() {
int N, M, K;
cin >> N >> M >> K;
// 友達関係の連鎖の関係にある人たちを同じグループとして表現する
init(N);
// map<pii, bool> following;
vector<int> As(M);
vector<int> Bs(M);
REP(i, M) {
int A;
int B;
cin >> A >> B;
// indへ
A--;
B--;
As.at(i) = A;
Bs.at(i) = B;
// following[mk(A,B)] = true;
// following[mk(B,A)] = true;
// AさんとBさんは同じクラスタに属する
unite(A, B);
}
// map<pii, bool> blocking;
vector<int> Cs(M);
vector<int> Ds(M);
REP(i, K) {
int C, D;
cin >> C >> D;
// indへ
C--;
D--;
Cs.at(i) = C;
Ds.at(i) = D;
// blocking[mk(C, D)] = true;
// blocking[mk(D, C)] = true;
}
vi same_and_following(N);
vi same_and_blocking(N);
REP(i, M) {
int A = As.at(i);
int B = Bs.at(i);
if (same(A, B)) {
same_and_following.at(A)++;
same_and_following.at(B)++;
}
}
REP(i, K) {
int C = Cs.at(i);
int D = Ds.at(i);
if (same(C, D)) {
same_and_blocking.at(C)++;
same_and_blocking.at(D)++;
}
}
// それぞれの人についてチェック
REP(i, N) {
int friend_sugg =
size(i) - same_and_following.at(i) - same_and_blocking.at(i) - 1;
cout << friend_sugg << " ";
}
cout << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// #define int ll
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<bool>> vvb;
typedef map<int, bool> mib;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n - 1); i >= 0; --i)
#define FORE(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << '\n'
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << '\n'
#define debug3(x, y, z) \
cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " \
<< z << '\n'
#define hyphen() cerr << "--\n"
#define ALL(vec) (vec).begin(), (vec).end()
#define REVALL(vec) (vec).rbegin(), (vec).rend()
static const int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
#define fst first
#define snd second
#define pb push_back
#define mk(x, y) make_pair((x), (y))
const int MOD = (int)1e9 + 7;
const int INF = numeric_limits<int>::max();
const int MAX_N = 100000 + 10;
vector<int> par(MAX_N);
vector<int> mysize(MAX_N);
vector<int> myrank(MAX_N);
void init(int n) {
REP(i, n) {
par.at(i) = i;
mysize.at(i) = 1;
myrank.at(i) = 0;
}
}
int root(int x) { return par.at(x) == x ? x : par.at(x) = root(par.at(x)); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (myrank.at(x) < myrank.at(y)) {
par.at(x) = y;
mysize.at(y) += mysize.at(x);
} else {
par.at(y) = x;
mysize.at(x) += mysize.at(y);
if (myrank.at(x) == myrank.at(y))
myrank.at(x)++;
}
}
int size(int x) { return mysize.at(root(x)); }
signed main() {
int N, M, K;
cin >> N >> M >> K;
// 友達関係の連鎖の関係にある人たちを同じグループとして表現する
init(N);
// map<pii, bool> following;
vector<int> As(M);
vector<int> Bs(M);
REP(i, M) {
int A;
int B;
cin >> A >> B;
// indへ
A--;
B--;
As.at(i) = A;
Bs.at(i) = B;
// following[mk(A,B)] = true;
// following[mk(B,A)] = true;
// AさんとBさんは同じクラスタに属する
unite(A, B);
}
// map<pii, bool> blocking;
vector<int> Cs(K);
vector<int> Ds(K);
REP(i, K) {
int C, D;
cin >> C >> D;
// indへ
C--;
D--;
Cs.at(i) = C;
Ds.at(i) = D;
// blocking[mk(C, D)] = true;
// blocking[mk(D, C)] = true;
}
vi same_and_following(N);
vi same_and_blocking(N);
REP(i, M) {
int A = As.at(i);
int B = Bs.at(i);
if (same(A, B)) {
same_and_following.at(A)++;
same_and_following.at(B)++;
}
}
REP(i, K) {
int C = Cs.at(i);
int D = Ds.at(i);
if (same(C, D)) {
same_and_blocking.at(C)++;
same_and_blocking.at(D)++;
}
}
// それぞれの人についてチェック
REP(i, N) {
int friend_sugg =
size(i) - same_and_following.at(i) - same_and_blocking.at(i) - 1;
cout << friend_sugg << " ";
}
cout << endl;
}
| replace | 115 | 117 | 115 | 117 | 0 | |
p02762 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;
#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
class UnionFind {
private:
vector<int> data;
public:
UnionFind(int size) : data(size, -1) {}
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
bool is_same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -data[root(x)]; }
bool unify(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
return false;
}
};
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<int> ans(N, -1);
vector<pair<int, int>> ab(M);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--, b--;
ab[i] = {a, b};
ans[a]--;
ans[b]--;
}
vector<pair<int, int>> cd(M);
for (int i = 0; i < K; i++) {
int c, d;
cin >> c >> d;
c--, d--;
cd[i] = {c, d};
}
UnionFind uf(N);
for (int i = 0; i < M; i++) {
const auto &[a, b] = ab[i];
uf.unify(a, b);
}
for (int i = 0; i < K; i++) {
const auto &[c, d] = cd[i];
if (uf.is_same(c, d)) {
ans[c]--;
ans[d]--;
}
}
for (int i = 0; i < N; i++) {
cout << uf.size(i) + ans[i] << "\n";
}
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;
#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
class UnionFind {
private:
vector<int> data;
public:
UnionFind(int size) : data(size, -1) {}
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
bool is_same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -data[root(x)]; }
bool unify(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
return false;
}
};
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<int> ans(N, -1);
vector<pair<int, int>> ab(M);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--, b--;
ab[i] = {a, b};
ans[a]--;
ans[b]--;
}
vector<pair<int, int>> cd(K);
for (int i = 0; i < K; i++) {
int c, d;
cin >> c >> d;
c--, d--;
cd[i] = {c, d};
}
UnionFind uf(N);
for (int i = 0; i < M; i++) {
const auto &[a, b] = ab[i];
uf.unify(a, b);
}
for (int i = 0; i < K; i++) {
const auto &[c, d] = cd[i];
if (uf.is_same(c, d)) {
ans[c]--;
ans[d]--;
}
}
for (int i = 0; i < N; i++) {
cout << uf.size(i) + ans[i] << "\n";
}
return 0;
}
| replace | 72 | 73 | 72 | 73 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<int> A(M), B(M), C(K), D(K);
for (int i = 0; i < M; i++)
cin >> A[i] >> B[i];
for (int i = 0; i < K; i++)
cin >> C[i] >> D[i];
// vector<vector<int>> VF(N+1, vector<int>(N+1));
vector<vector<int>> VF(N + 1);
UnionFind tree(N);
for (int i = 0; i < M; i++) {
VF[A[i]].push_back(B[i]);
VF[B[i]].push_back(A[i]);
tree.unite(A[i], B[i]);
}
for (int i = 0; i < K; i++) {
if (tree.same(C[i], D[i])) {
VF[C[i]].push_back(D[i]);
VF[D[i]].push_back(C[i]);
}
}
vector<int> GN(N + 1);
for (int i = 1; i < N + 1; i++) {
int r = tree.root(i);
GN[r]++;
}
for (int i = 1; i <= N; i++) {
int r = tree.root(i);
GN[i] = GN[r];
}
for (int i = 1; i < N + 1; i++) {
int n = VF[i].size();
int cnt = 0;
// for(int j = 0; j < n; j++){
// int x = VF[i][j];
// if(tree.same(i, x)) cnt++;
// }
cout << GN[i] - n - 1 << " ";
}
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
for (int i = 0; i < N; i++)
par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); // xの根をrx
int ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<int> A(M), B(M), C(K), D(K);
for (int i = 0; i < M; i++)
cin >> A[i] >> B[i];
for (int i = 0; i < K; i++)
cin >> C[i] >> D[i];
// vector<vector<int>> VF(N+1, vector<int>(N+1));
vector<vector<int>> VF(N + 1);
UnionFind tree(N + 1);
for (int i = 0; i < M; i++) {
VF[A[i]].push_back(B[i]);
VF[B[i]].push_back(A[i]);
tree.unite(A[i], B[i]);
}
for (int i = 0; i < K; i++) {
if (tree.same(C[i], D[i])) {
VF[C[i]].push_back(D[i]);
VF[D[i]].push_back(C[i]);
}
}
vector<int> GN(N + 1);
for (int i = 1; i < N + 1; i++) {
int r = tree.root(i);
GN[r]++;
}
for (int i = 1; i <= N; i++) {
int r = tree.root(i);
GN[i] = GN[r];
}
for (int i = 1; i < N + 1; i++) {
int n = VF[i].size();
int cnt = 0;
// for(int j = 0; j < n; j++){
// int x = VF[i][j];
// if(tree.same(i, x)) cnt++;
// }
cout << GN[i] - n - 1 << " ";
}
cout << endl;
} | replace | 44 | 45 | 44 | 45 | 0 | |
p02762 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;
struct Union_find {
vector<int> par;
vector<int> cnt;
Union_find(int p) {
par = vector<int>(p + 1);
cnt = vector<int>(p + 1, 1);
for (int i = 1; i <= p; i++) {
par[i] = i;
}
}
int find(int x) {
if (par[x] == x)
return x;
else {
return par[x] = find(par[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (cnt[x] < cnt[y]) {
par[x] = y;
cnt[y] += cnt[x];
} else {
par[y] = x;
cnt[x] += cnt[y];
}
}
bool same(int x, int y) { return find(x) == find(y); }
int get_cnt(int x) { return cnt[find(x)]; }
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<bool>> b(n, vector<bool>(n + 1, false));
vector<int> c(n + 1);
Union_find f(n + 1);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
f.unite(x, y);
c[x]++;
c[y]++;
}
vector<int> ans(n + 1);
for (int i = 1; i <= n; i++) {
ans[i] = f.get_cnt(i) - c[i] - 1;
}
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
if (f.same(x, y)) {
ans[x]--;
ans[y]--;
}
}
cout << ans[1];
for (int i = 2; i <= n; i++) {
cout << " " << ans[i];
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = (long long)1e9 + 7;
struct Union_find {
vector<int> par;
vector<int> cnt;
Union_find(int p) {
par = vector<int>(p + 1);
cnt = vector<int>(p + 1, 1);
for (int i = 1; i <= p; i++) {
par[i] = i;
}
}
int find(int x) {
if (par[x] == x)
return x;
else {
return par[x] = find(par[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (cnt[x] < cnt[y]) {
par[x] = y;
cnt[y] += cnt[x];
} else {
par[y] = x;
cnt[x] += cnt[y];
}
}
bool same(int x, int y) { return find(x) == find(y); }
int get_cnt(int x) { return cnt[find(x)]; }
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> c(n + 1);
Union_find f(n + 1);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
f.unite(x, y);
c[x]++;
c[y]++;
}
vector<int> ans(n + 1);
for (int i = 1; i <= n; i++) {
ans[i] = f.get_cnt(i) - c[i] - 1;
}
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
if (f.same(x, y)) {
ans[x]--;
ans[y]--;
}
}
cout << ans[1];
for (int i = 2; i <= n; i++) {
cout << " " << ans[i];
}
cout << endl;
return 0;
}
| delete | 46 | 47 | 46 | 46 | MLE | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printVec(v) \
printf("{"); \
for (const auto &i : v) { \
std::cout << i << ", "; \
} \
printf("}\n");
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using P = pair<int, int>;
using ll = long long;
const ll INF = 1LL << 60;
const double PI = 3.1415926535897932;
const int MOD = 1e9 + 7;
// cin.tie(0);ios::sync_with_stdio(false);
vector<vector<int>> fri(100005);
vector<vector<int>> blo(100005);
int main() {
int n, m, k;
cin >> n >> m >> k;
rep(i, m) {
int a, b;
cin >> a >> b;
fri[a].push_back(b);
fri[b].push_back(a);
}
rep(i, k) {
int c, d;
cin >> c >> d;
blo[c].push_back(d);
blo[d].push_back(c);
}
vector<int> group(100);
map<int, int> mi;
for (int i = 1; i <= n; i++) {
if (group[i] == 0) {
queue<int> qi;
qi.push(i);
group[i] = i;
mi[i]++;
while (!qi.empty()) {
int now = qi.front();
qi.pop();
for (auto next_v : fri[now]) {
if (group[next_v] == 0) {
group[next_v] = i;
mi[i]++;
qi.push(next_v);
}
}
}
}
}
for (int i = 1; i <= n; i++) {
int ans = 0;
ans += mi[group[i]] - (1 + fri[i].size());
for (auto v : blo[i]) {
if (group[i] == group[v])
ans--;
}
cout << ans << " ";
}
cout << endl;
// printVec(group);
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define printVec(v) \
printf("{"); \
for (const auto &i : v) { \
std::cout << i << ", "; \
} \
printf("}\n");
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using P = pair<int, int>;
using ll = long long;
const ll INF = 1LL << 60;
const double PI = 3.1415926535897932;
const int MOD = 1e9 + 7;
// cin.tie(0);ios::sync_with_stdio(false);
vector<vector<int>> fri(100005);
vector<vector<int>> blo(100005);
int main() {
int n, m, k;
cin >> n >> m >> k;
rep(i, m) {
int a, b;
cin >> a >> b;
fri[a].push_back(b);
fri[b].push_back(a);
}
rep(i, k) {
int c, d;
cin >> c >> d;
blo[c].push_back(d);
blo[d].push_back(c);
}
vector<int> group(100005);
map<int, int> mi;
for (int i = 1; i <= n; i++) {
if (group[i] == 0) {
queue<int> qi;
qi.push(i);
group[i] = i;
mi[i]++;
while (!qi.empty()) {
int now = qi.front();
qi.pop();
for (auto next_v : fri[now]) {
if (group[next_v] == 0) {
group[next_v] = i;
mi[i]++;
qi.push(next_v);
}
}
}
}
}
for (int i = 1; i <= n; i++) {
int ans = 0;
ans += mi[group[i]] - (1 + fri[i].size());
for (auto v : blo[i]) {
if (group[i] == group[v])
ans--;
}
cout << ans << " ";
}
cout << endl;
// printVec(group);
return 0;
}
| replace | 52 | 53 | 52 | 53 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1000000;
struct Unionfind {
vector<int> parent;
Unionfind(int N) { parent = vector<int>(N, -1); }
int root(int x) { // 親の番号を検索する
if (parent[x] < 0)
return x;
return parent[x] = root(parent[x]);
}
int size(int x) { // 自分のグループの大きさを返す
return -parent[root(x)];
}
bool unite(int x, int y) { // XとYを同じグループにする
x = root(x);
y = root(y);
if (x == y)
return false;
if (size(x) < size(y))
swap(x, y);
parent[x] += parent[y];
parent[y] = x;
return true;
}
bool same(int x, int y) {
if (root(x) == root(y))
return true;
return false;
}
};
int fri[100005];
vector<int> bl[100005];
int main() {
int N, M, K;
cin >> N >> M >> K;
Unionfind uni(N);
rep(i, M) {
int a, b;
cin >> a >> b;
a--;
b--;
fri[a]++;
fri[b]++;
uni.unite(a, b);
}
rep(i, K) {
int a, b;
cin >> a >> b;
a--;
b--;
bl[a].push_back(b);
bl[b].push_back(a);
}
rep(i, N) {
int ans;
ans = uni.size(i) - 1 - fri[i];
for (int u : bl[i]) {
if (uni.same(i, bl[i][u]))
ans--;
}
cout << ans << " ";
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define P pair<int, int>
#define PI 3.141592653589793
const int INF = 1000000;
struct Unionfind {
vector<int> parent;
Unionfind(int N) { parent = vector<int>(N, -1); }
int root(int x) { // 親の番号を検索する
if (parent[x] < 0)
return x;
return parent[x] = root(parent[x]);
}
int size(int x) { // 自分のグループの大きさを返す
return -parent[root(x)];
}
bool unite(int x, int y) { // XとYを同じグループにする
x = root(x);
y = root(y);
if (x == y)
return false;
if (size(x) < size(y))
swap(x, y);
parent[x] += parent[y];
parent[y] = x;
return true;
}
bool same(int x, int y) {
if (root(x) == root(y))
return true;
return false;
}
};
int fri[100005];
vector<int> bl[100005];
int main() {
int N, M, K;
cin >> N >> M >> K;
Unionfind uni(N);
rep(i, M) {
int a, b;
cin >> a >> b;
a--;
b--;
fri[a]++;
fri[b]++;
uni.unite(a, b);
}
rep(i, K) {
int a, b;
cin >> a >> b;
a--;
b--;
bl[a].push_back(b);
bl[b].push_back(a);
}
rep(i, N) {
int ans;
ans = uni.size(i) - 1 - fri[i];
for (int u : bl[i]) {
if (uni.same(i, u))
ans--;
}
cout << ans << " ";
}
}
| replace | 68 | 69 | 68 | 69 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <list>
#include <unordered_map>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 1e9
using namespace std;
typedef long long ll;
// class to represent a disjoint set
class DisjointSet {
unordered_map<int, int> parent;
public:
unordered_map<int, int> size;
// perform MakeSet operation
void makeSet(vector<int> const &universe) {
// create n disjoint sets (one for each item)
for (int i : universe) {
parent[i] = i;
size[i] = 1;
}
}
// Find the root of the set in which element k belongs
int Find(int k) {
if (parent[k] == k)
return k; // if k is root
return Find(parent[k]); // recur for parent until we find root
}
// Perform Union of two subsets
void Union(int a, int b) {
// find root of the sets in which elements x and y belongs
int x = Find(a);
int y = Find(b);
parent[x] = y;
// increment size when union happens
if (x != y)
size[y] += size[x];
}
// Get number of elements in the set which a belongs to
int getSize(int a) {
int x = Find(a);
return size[x];
}
};
int main() {
int n, m, k, a, b, c, d;
cin >> n >> m >> k;
DisjointSet ds;
vector<int> recs;
REP(i, n) recs.push_back(i + 1);
ds.makeSet(recs);
vector<int> f(n, 0);
map<int, vector<int>> bl;
REP(j, m) {
cin >> c >> d;
ds.Union(c, d);
// fb[c].push_back(d);
// fb[d].push_back(c);
f[c - 1]++;
f[d - 1]++;
}
REP(i, k) {
cin >> a >> b;
bl[a].push_back(b);
bl[b].push_back(a);
}
// vector<int> sizee(n, 0);
// REP(i, n) sizee[i] = ds.getSize(i+1);
int ans;
REP(i, n) {
ans = ds.size[ds.Find(i + 1)] - 1;
ans -= f[i];
for (auto x : bl[i + 1]) {
if (ds.Find(x) == ds.Find(i + 1)) {
ans--;
}
}
cout << ans << " ";
}
cout << endl;
} | #include <bits/stdc++.h>
#include <list>
#include <unordered_map>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 1e9
using namespace std;
typedef long long ll;
// class to represent a disjoint set
class DisjointSet {
unordered_map<int, int> parent;
public:
unordered_map<int, int> size;
// perform MakeSet operation
void makeSet(vector<int> const &universe) {
// create n disjoint sets (one for each item)
for (int i : universe) {
parent[i] = i;
size[i] = 1;
}
}
// Find the root of the set in which element k belongs
int Find(int k) {
if (parent[k] == k)
return k; // if k is root
return parent[k] = Find(parent[k]); // recur for parent until we find root
}
// Perform Union of two subsets
void Union(int a, int b) {
// find root of the sets in which elements x and y belongs
int x = Find(a);
int y = Find(b);
parent[x] = y;
// increment size when union happens
if (x != y)
size[y] += size[x];
}
// Get number of elements in the set which a belongs to
int getSize(int a) {
int x = Find(a);
return size[x];
}
};
int main() {
int n, m, k, a, b, c, d;
cin >> n >> m >> k;
DisjointSet ds;
vector<int> recs;
REP(i, n) recs.push_back(i + 1);
ds.makeSet(recs);
vector<int> f(n, 0);
map<int, vector<int>> bl;
REP(j, m) {
cin >> c >> d;
ds.Union(c, d);
// fb[c].push_back(d);
// fb[d].push_back(c);
f[c - 1]++;
f[d - 1]++;
}
REP(i, k) {
cin >> a >> b;
bl[a].push_back(b);
bl[b].push_back(a);
}
// vector<int> sizee(n, 0);
// REP(i, n) sizee[i] = ds.getSize(i+1);
int ans;
REP(i, n) {
ans = ds.size[ds.Find(i + 1)] - 1;
ans -= f[i];
for (auto x : bl[i + 1]) {
if (ds.Find(x) == ds.Find(i + 1)) {
ans--;
}
}
cout << ans << " ";
}
cout << endl;
} | replace | 29 | 31 | 29 | 31 | TLE | |
p02762 | Python | Time Limit Exceeded | import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
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):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())
def main():
N, M, K = [int(x) for x in input().split()]
u = UnionFind(N)
ans1 = [0] * N
for _ in range(M):
a, b = [int(x) for x in input().split()]
ans1[a - 1] += 1
ans1[b - 1] += 1
u.union(a - 1, b - 1)
ans2 = [0] * N
for _ in range(K):
a, b = [int(x) for x in input().split()]
if u.same(a - 1, b - 1):
ans2[a - 1] += 1
ans2[b - 1] += 1
for i in range(N):
# friblo[i].add(i)
# print(u.members(i))
# print(friblo[i])
# print(len(set(u.members(i)) - friblo[i]), end=" ")
print(len(u.members(i)) - ans1[i] - ans2[i] - 1, end=" ")
if __name__ == "__main__":
main()
| import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
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):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())
def main():
N, M, K = [int(x) for x in input().split()]
u = UnionFind(N)
ans1 = [0] * N
for _ in range(M):
a, b = [int(x) for x in input().split()]
ans1[a - 1] += 1
ans1[b - 1] += 1
u.union(a - 1, b - 1)
ans2 = [0] * N
for _ in range(K):
a, b = [int(x) for x in input().split()]
if u.same(a - 1, b - 1):
ans2[a - 1] += 1
ans2[b - 1] += 1
for i in range(N):
# friblo[i].add(i)
# print(u.members(i))
# print(friblo[i])
# print(len(set(u.members(i)) - friblo[i]), end=" ")
print(u.size(i) - ans1[i] - ans2[i] - 1, end=" ")
if __name__ == "__main__":
main()
| replace | 78 | 79 | 78 | 79 | TLE | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void dfs(int group_num, int now, vector<vector<int>> fr, vector<int> &group) {
/*深さ有線探索
group_num:見る頂点のグループ番号
now:見る頂点の頂点番号
fr:友人関係のグラフの隣接リスト表現
group:頂点のグループ番号を格納した配列
*/
group.at(now) = group_num;
for (auto p : fr.at(now)) {
// nowと友人関係にある頂点を探索
if (group.at(p) == -1) {
// まだグループ分けされていない
dfs(group_num, p, fr, group);
}
}
return;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
/*
fr:友人関係のグラフの隣接リスト表現
fr_num:それぞれの人の友人の数
bl:ブロック関係をまとめた配列
*/
vector<vector<int>> fr(n);
vector<int> fr_num(n, 0);
vector<pair<int, int>> bl(k);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
fr.at(a).push_back(b);
fr.at(b).push_back(a);
fr_num.at(a)++;
fr_num.at(b)++;
}
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
pair<int, int> p;
p.first = c;
p.second = d;
bl.at(i) = p;
}
// group: 各頂点のグループ番号を格納する配列。-1で初期化
vector<int> group(n, -1);
for (int i = 0; i < n; i++) {
if (group.at(i) == -1) {
dfs(i, i, fr, group);
}
}
// group_num:各グループにいくつの頂点が含まれているか
vector<int> group_num(n, 0);
for (int i = 0; i < n; i++) {
group_num.at(group.at(i))++;
}
// ans:答えを格納する配列
vector<int> ans(n);
for (int i = 0; i < n; i++) {
// 自分のグループの要素数 - すでに友達の人数 - 自分
ans.at(i) = group_num.at(group.at(i)) - fr_num.at(i) - 1;
}
for (int i = 0; i < k; i++) {
int c, d;
// ブロック関係にあるけど同じグループの人を引く。
c = bl.at(i).first;
d = bl.at(i).second;
if (group.at(c) == group.at(d)) {
ans.at(c)--;
ans.at(d)--;
}
}
for (int i = 0; i < n; i++) {
cout << ans.at(i) << ' ';
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void dfs(int group_num, int now, vector<vector<int>> &fr, vector<int> &group) {
/*深さ有線探索
group_num:見る頂点のグループ番号
now:見る頂点の頂点番号
fr:友人関係のグラフの隣接リスト表現
group:頂点のグループ番号を格納した配列
*/
group.at(now) = group_num;
for (auto p : fr.at(now)) {
// nowと友人関係にある頂点を探索
if (group.at(p) == -1) {
// まだグループ分けされていない
dfs(group_num, p, fr, group);
}
}
return;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
/*
fr:友人関係のグラフの隣接リスト表現
fr_num:それぞれの人の友人の数
bl:ブロック関係をまとめた配列
*/
vector<vector<int>> fr(n);
vector<int> fr_num(n, 0);
vector<pair<int, int>> bl(k);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
fr.at(a).push_back(b);
fr.at(b).push_back(a);
fr_num.at(a)++;
fr_num.at(b)++;
}
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
pair<int, int> p;
p.first = c;
p.second = d;
bl.at(i) = p;
}
// group: 各頂点のグループ番号を格納する配列。-1で初期化
vector<int> group(n, -1);
for (int i = 0; i < n; i++) {
if (group.at(i) == -1) {
dfs(i, i, fr, group);
}
}
// group_num:各グループにいくつの頂点が含まれているか
vector<int> group_num(n, 0);
for (int i = 0; i < n; i++) {
group_num.at(group.at(i))++;
}
// ans:答えを格納する配列
vector<int> ans(n);
for (int i = 0; i < n; i++) {
// 自分のグループの要素数 - すでに友達の人数 - 自分
ans.at(i) = group_num.at(group.at(i)) - fr_num.at(i) - 1;
}
for (int i = 0; i < k; i++) {
int c, d;
// ブロック関係にあるけど同じグループの人を引く。
c = bl.at(i).first;
d = bl.at(i).second;
if (group.at(c) == group.at(d)) {
ans.at(c)--;
ans.at(d)--;
}
}
for (int i = 0; i < n; i++) {
cout << ans.at(i) << ' ';
}
}
| replace | 4 | 5 | 4 | 5 | TLE | |
p02762 | C++ | Time Limit Exceeded | // #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <array>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#include <cassert>
#include <chrono>
#include <cmath>
#include <functional>
#include <iomanip>
#include <numeric>
#include <random>
#include <memory>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
// typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
typedef pair<long double, int> pdbi;
typedef pair<int, pii> pipii;
typedef pair<ll, pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
template <class T>
using MINPQ = std::priority_queue<T, vector<T>, greater<T>>; // MINPQ<pii> pq;
template <class T> using MAXPQ = std::priority_queue<T>;
const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 62;
const long double EPS = 1e-12;
const ll mod = 1000000000 + 7;
// const ll mod = 998244353;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const long double pi = 3.141592653589793;
#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod
#define inside(y, x, h, w) \
(0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false
#define stand(B, i) \
(((B >> i) & 1) == 1) ? true : false // Bのi番目のbitが1ならtrue, 0ならfalse
// debug
#define DEBUG
#define DUMPOUT cerr
#ifdef DEBUG
#define dump(...) \
DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \
<< endl; \
DUMPOUT << " "; \
dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
void dump_func() { DUMPOUT << endl; };
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) == 0)
DUMPOUT << " ";
else
DUMPOUT << ", ";
dump_func(std::move(tail)...);
}
// ostream
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "[";
for (int i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
os << "]";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "[";
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "[";
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "]";
return os;
}
struct UnionFind {
vector<int> par,
rank; // rank[i]:=頂点iの属する根からの深さ(縮約で変更はしない)
int cmps; // 連結成分の総数
vector<set<int>> cmp;
UnionFind() {}
UnionFind(int n) : par(n), rank(n, 1) {
cmps = n;
cmp.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
cmp[i].insert(i);
}
}
int find_root(int now) {
if (par[now] == now)
return now;
return par[now] = find_root(par[now]);
}
void unite(int u, int v) {
int x = find_root(u);
int y = find_root(v);
if (x == y)
return;
if (rank[x] < rank[y])
swap(x, y);
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
cmps--;
for (auto z : cmp[y]) {
cmp[x].insert(z);
}
cmp[y].clear();
}
bool same(int u, int v) { return find_root(u) == find_root(v); }
};
int n, m, k;
vector<vector<int>> f, e;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> k;
f.resize(n);
e.resize(n);
UnionFind uf(n);
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
uf.unite(a, b);
f[a].push_back(b);
f[b].push_back(a);
}
rep(i, k) {
int c, d;
cin >> c >> d;
c--;
d--;
e[c].push_back(d);
e[d].push_back(c);
}
vector<int> ANS;
rep(i, n) {
// cerr << i << endl;
int r = uf.find_root(i);
set<int> S = uf.cmp[r];
int ans = S.size();
ans--;
ans -= f[i].size();
for (auto x : e[i]) {
if (S.find(x) == S.end())
continue;
ans--;
}
ANS.push_back(ans);
}
rep(i, n) {
if (i == n - 1)
cout << ANS[i] << endl;
else
cout << ANS[i] << " ";
}
}
| // #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <array>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
#include <cassert>
#include <chrono>
#include <cmath>
#include <functional>
#include <iomanip>
#include <numeric>
#include <random>
#include <memory>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
// typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
typedef pair<long double, int> pdbi;
typedef pair<int, pii> pipii;
typedef pair<ll, pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
template <class T>
using MINPQ = std::priority_queue<T, vector<T>, greater<T>>; // MINPQ<pii> pq;
template <class T> using MAXPQ = std::priority_queue<T>;
const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 62;
const long double EPS = 1e-12;
const ll mod = 1000000000 + 7;
// const ll mod = 998244353;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const long double pi = 3.141592653589793;
#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod
#define inside(y, x, h, w) \
(0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false
#define stand(B, i) \
(((B >> i) & 1) == 1) ? true : false // Bのi番目のbitが1ならtrue, 0ならfalse
// debug
#define DEBUG
#define DUMPOUT cerr
#ifdef DEBUG
#define dump(...) \
DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \
<< endl; \
DUMPOUT << " "; \
dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
void dump_func() { DUMPOUT << endl; };
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) == 0)
DUMPOUT << " ";
else
DUMPOUT << ", ";
dump_func(std::move(tail)...);
}
// ostream
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "[";
for (int i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
os << "]";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "[";
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "]";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "[";
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "]";
return os;
}
struct UnionFind {
vector<int> par,
rank; // rank[i]:=頂点iの属する根からの深さ(縮約で変更はしない)
int cmps; // 連結成分の総数
vector<set<int>> cmp;
UnionFind() {}
UnionFind(int n) : par(n), rank(n, 1) {
cmps = n;
cmp.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
cmp[i].insert(i);
}
}
int find_root(int now) {
if (par[now] == now)
return now;
return par[now] = find_root(par[now]);
}
void unite(int u, int v) {
int x = find_root(u);
int y = find_root(v);
if (x == y)
return;
if (rank[x] < rank[y])
swap(x, y);
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
cmps--;
for (auto z : cmp[y]) {
cmp[x].insert(z);
}
cmp[y].clear();
}
bool same(int u, int v) { return find_root(u) == find_root(v); }
};
int n, m, k;
vector<vector<int>> f, e;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> k;
f.resize(n);
e.resize(n);
UnionFind uf(n);
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
uf.unite(a, b);
f[a].push_back(b);
f[b].push_back(a);
}
rep(i, k) {
int c, d;
cin >> c >> d;
c--;
d--;
e[c].push_back(d);
e[d].push_back(c);
}
vector<int> ANS;
rep(i, n) {
// cerr << i << endl;
int r = uf.find_root(i);
set<int> &S = uf.cmp[r];
int ans = S.size();
ans--;
ans -= f[i].size();
for (auto x : e[i]) {
if (S.find(x) == S.end())
continue;
ans--;
}
ANS.push_back(ans);
}
rep(i, n) {
if (i == n - 1)
cout << ANS[i] << endl;
else
cout << ANS[i] << " ";
}
}
| replace | 228 | 229 | 228 | 229 | TLE | |
p02762 | C++ | Runtime Error | // --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll, ll>
#define vpii vector<pair<ll, ll>>
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a, i) memset(a, i, sizeof(a))
#define all(x) x.begin(), x.end()
#define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define reprr(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x, ll y, ll z) { return max(max(x, y), z); }
ll min3(ll x, ll y, ll z) { return min(min(x, y), z); }
const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7,
inf = 1e17 + 10;
const int INF = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
#define trace(...) \
cerr << "Line:" << __LINE__ << " "; \
__f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space
ll power(ll x, ll n) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = ((x % mod) * (x % mod)) % mod;
n = n / 2;
}
return result;
}
int n, m, k;
int a[N], b[N], c[N], d[N];
int cnt[N];
int fri[N];
struct dsu {
static const int MAX_N = 100005; // nodes
int parent[MAX_N];
int size[MAX_N];
void make_sets() {
for (int i = 0; i < MAX_N; i++) {
parent[i] = i;
size[i] = 1;
}
}
int find_set(int u) {
if (parent[u] == u)
return u;
else
return parent[u] = find_set(parent[u]);
}
void make_union(int u, int v) {
u = find_set(u);
v = find_set(v);
if (u == v)
return;
if (size[u] < size[v])
swap(u, v);
parent[v] = u;
size[u] += size[v];
}
bool same_set(int u, int v) { return find_set(u) == find_set(v); }
};
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
dsu p;
p.make_sets();
rep(i, 1, m + 1) {
cin >> a[i] >> b[i];
fri[a[i]]++;
fri[b[i]]++;
p.make_union(a[i], b[i]);
}
rep(i, 1, k + 1) {
cin >> c[i] >> d[i];
if (p.find_set(c[i]) == p.find_set(d[i]))
cnt[c[i]]++, cnt[d[i]]++;
}
rep(i, 1, n + 1) {
int x = p.find_set(i);
cout << p.size[x] - cnt[i] - fri[i] - 1 << " ";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll, ll>
#define vpii vector<pair<ll, ll>>
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a, i) memset(a, i, sizeof(a))
#define all(x) x.begin(), x.end()
#define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define reprr(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x, ll y, ll z) { return max(max(x, y), z); }
ll min3(ll x, ll y, ll z) { return min(min(x, y), z); }
const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7,
inf = 1e17 + 10;
const int INF = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
#define trace(...) \
cerr << "Line:" << __LINE__ << " "; \
__f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space
ll power(ll x, ll n) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = ((x % mod) * (x % mod)) % mod;
n = n / 2;
}
return result;
}
int n, m, k;
int a[N], b[N], c[N], d[N];
int cnt[N];
int fri[N];
struct dsu {
static const int MAX_N = 100005; // nodes
int parent[MAX_N];
int size[MAX_N];
void make_sets() {
for (int i = 0; i < MAX_N; i++) {
parent[i] = i;
size[i] = 1;
}
}
int find_set(int u) {
if (parent[u] == u)
return u;
else
return parent[u] = find_set(parent[u]);
}
void make_union(int u, int v) {
u = find_set(u);
v = find_set(v);
if (u == v)
return;
if (size[u] < size[v])
swap(u, v);
parent[v] = u;
size[u] += size[v];
}
bool same_set(int u, int v) { return find_set(u) == find_set(v); }
};
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
dsu p;
p.make_sets();
rep(i, 1, m + 1) {
cin >> a[i] >> b[i];
fri[a[i]]++;
fri[b[i]]++;
p.make_union(a[i], b[i]);
}
rep(i, 1, k + 1) {
cin >> c[i] >> d[i];
if (p.find_set(c[i]) == p.find_set(d[i]))
cnt[c[i]]++, cnt[d[i]]++;
}
rep(i, 1, n + 1) {
int x = p.find_set(i);
cout << p.size[x] - cnt[i] - fri[i] - 1 << " ";
}
return 0;
} | delete | 0 | 11 | 0 | 0 | 0 | |
p02762 | C++ | Runtime Error | #include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
struct UnionFind {
vector<int> d;
UnionFind(int n) : d(n, -1) {}
int root(int x) {
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
};
// 直接友達な数
int deg[2002] = {0};
// to[i]がもつブロック関係な人
vector<int> to[100005];
int main(void) {
int n, m, k;
cin >> n >> m >> k;
// m--;k--;
UnionFind node(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
node.unite(a, b);
}
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
to[c].push_back(d);
to[d].push_back(c);
}
for (int i = 0; i < n; i++) {
int ans = node.size(i) - 1 - deg[i];
for (int u : to[i]) {
// iがブロック関係の人数分引く
if (node.same(i, u))
--ans;
}
printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
}
return 0;
} | #include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
struct UnionFind {
vector<int> d;
UnionFind(int n) : d(n, -1) {}
int root(int x) {
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
};
// 直接友達な数
int deg[100001] = {0};
// to[i]がもつブロック関係な人
vector<int> to[100005];
int main(void) {
int n, m, k;
cin >> n >> m >> k;
// m--;k--;
UnionFind node(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
node.unite(a, b);
}
for (int i = 0; i < k; i++) {
int c, d;
cin >> c >> d;
c--;
d--;
to[c].push_back(d);
to[d].push_back(c);
}
for (int i = 0; i < n; i++) {
int ans = node.size(i) - 1 - deg[i];
for (int u : to[i]) {
// iがブロック関係の人数分引く
if (node.same(i, u))
--ans;
}
printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
}
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int par[100003], fz[100005], bz[100005], cnt, dsz[100005];
vector<int> adj[100005], bdj[100005];
int dfs(int src, int pr) {
par[src] = pr;
int sz = fz[src];
cnt++;
for (int lp = 0; lp < sz; lp++) {
int u = adj[src][lp];
if (par[u] == 0) {
dfs(u, pr);
}
}
}
int main() {
int n, m, k, a, b, c, i, j, fr, sc, tr;
scanf("%d %d %d", &n, &m, &k);
for (i = 1; i <= m; i++) {
scanf("%d %d", &a, &b);
adj[a].push_back(b);
fz[a]++;
adj[b].push_back(a);
fz[b]++;
}
for (i = 1; i <= k; i++) {
scanf("%d %d", &a, &b);
bdj[a].push_back(b);
bz[a]++;
bdj[b].push_back(a);
bz[b]++;
}
for (i = 1; i <= n; i++) {
if (par[i] == 0) {
cnt = 0;
dfs(i, i);
cnt--;
dsz[i] = cnt;
}
}
for (i = 1; i <= n; i++) {
fr = dsz[par[i]] - fz[i];
int sz = bz[i];
for (int lp = 0; lp < sz; lp++) {
int u = bdj[i][lp];
if (par[u] == par[i])
fr--;
if (fr == 0)
break;
}
printf("%d ", fr);
// cout<<i<<" "<<dsz[par[i]]<<" "<<fz[i]<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int par[100003], fz[100005], bz[100005], cnt, dsz[100005];
vector<int> adj[100005], bdj[100005];
void dfs(int src, int pr) {
par[src] = pr;
int sz = fz[src];
cnt++;
for (int lp = 0; lp < sz; lp++) {
int u = adj[src][lp];
if (par[u] == 0) {
dfs(u, pr);
}
}
}
int main() {
int n, m, k, a, b, c, i, j, fr, sc, tr;
scanf("%d %d %d", &n, &m, &k);
for (i = 1; i <= m; i++) {
scanf("%d %d", &a, &b);
adj[a].push_back(b);
fz[a]++;
adj[b].push_back(a);
fz[b]++;
}
for (i = 1; i <= k; i++) {
scanf("%d %d", &a, &b);
bdj[a].push_back(b);
bz[a]++;
bdj[b].push_back(a);
bz[b]++;
}
for (i = 1; i <= n; i++) {
if (par[i] == 0) {
cnt = 0;
dfs(i, i);
cnt--;
dsz[i] = cnt;
}
}
for (i = 1; i <= n; i++) {
fr = dsz[par[i]] - fz[i];
int sz = bz[i];
for (int lp = 0; lp < sz; lp++) {
int u = bdj[i][lp];
if (par[u] == par[i])
fr--;
if (fr == 0)
break;
}
printf("%d ", fr);
// cout<<i<<" "<<dsz[par[i]]<<" "<<fz[i]<<endl;
}
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
const int MAX_N = 100001;
int par[MAX_N];
int rank[MAX_N];
int nele[MAX_N];
void init() {
for (int i = 0; i < MAX_N; ++i) {
par[i] = i;
rank[i] = 0;
nele[i] = 1;
}
}
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
nele[x] += nele[y];
nele[y] = nele[x];
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y])
++rank[x];
}
return;
}
bool same(int x, int y) { return root(x) == root(y); }
int main() {
init();
int N, M, K;
std::cin >> N >> M >> K;
int A, B;
int old_friend[N];
std::fill(old_friend, old_friend + N, 0);
for (int i = 0; i < M; ++i) {
std::cin >> A >> B;
--A;
--B;
unite(A, B);
++old_friend[A];
++old_friend[B];
}
std::vector<int> blocked[N];
int C, D;
for (int i = 0; i < K; ++i) {
std::cin >> C >> D;
--C;
--D;
blocked[C].push_back(D);
blocked[D].push_back(C);
}
for (int i = 0; i < N; ++i) {
int count = 0;
for (int j = 0; j < N; ++j) {
if (same(i, j))
++count;
}
for (int j = 0; j < blocked[i].size(); ++j) {
if (same(i, blocked[i][j]))
--count;
}
count -= old_friend[i] + 1;
std::cout << count << ' ';
}
std::cout << std::endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
const int MAX_N = 100001;
int par[MAX_N];
int rank[MAX_N];
int nele[MAX_N];
void init() {
for (int i = 0; i < MAX_N; ++i) {
par[i] = i;
rank[i] = 0;
nele[i] = 1;
}
}
int root(int x) {
if (par[x] == x)
return x;
else
return par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
nele[x] += nele[y];
nele[y] = nele[x];
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y])
++rank[x];
}
return;
}
bool same(int x, int y) { return root(x) == root(y); }
int main() {
init();
int N, M, K;
std::cin >> N >> M >> K;
int A, B;
int old_friend[N];
std::fill(old_friend, old_friend + N, 0);
for (int i = 0; i < M; ++i) {
std::cin >> A >> B;
--A;
--B;
unite(A, B);
++old_friend[A];
++old_friend[B];
}
std::vector<int> blocked[N];
int C, D;
for (int i = 0; i < K; ++i) {
std::cin >> C >> D;
--C;
--D;
blocked[C].push_back(D);
blocked[D].push_back(C);
}
for (int i = 0; i < N; ++i) {
int count = nele[root(i)];
for (int j = 0; j < blocked[i].size(); ++j) {
if (same(i, blocked[i][j]))
--count;
}
count -= old_friend[i] + 1;
std::cout << count << ' ';
}
std::cout << std::endl;
return 0;
}
| replace | 73 | 78 | 73 | 74 | TLE | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using llint = long long int;
#define rep(i, c) for (int i = 0; i < (int)c; ++i)
// c 回繰り返す
//////////////////////////クラス構造体///////////////////////////
class UnionFind { // UnionFind木 Nの数に注意 0=<x<N まで作られる
private:
vector<int> par;
public:
UnionFind(int N) : par(N, -1) {} // 初期化コンストラクタ 根を初期化
// par[i]は根のとき負 それ以外非負
int root(int x) { // データxの根を返す root(i)は必ず非負
if (par[x] < 0)
return x; // par[x]が負のときxは木の根である
return par[x] = root(par[x]); // 経路圧縮
}
void unite(int x, int y) { // xの根とyの根を同じにする
int rx = root(x);
int ry = root(y);
if (rx == ry)
return; // xの根とyの根が同じとき なにもしない
if (par[rx] > par[ry])
swap(rx, ry); // ランク処理
par[rx] += par[ry];
par[ry] = rx; // 違うとき 根をおなじにする
}
bool same(int x, int y) { // 根が同じとき TRUE
return root(x) == root(y);
}
int size(int x) { return -par[root(x)]; } // サイズを返す
};
///////////////////////////////////////////////////////////
int main() {
int N, M, K;
cin >> N >> M >> K;
UnionFind frie(N);
int count[100010];
vector<int> v[10010];
rep(i, M) {
int A, B;
cin >> A >> B;
--A;
--B;
count[A]++;
count[B]++;
frie.unite(A, B);
}
rep(i, K) {
int C, D;
cin >> C >> D;
--C;
--D;
v[C].push_back(D);
v[D].push_back(C);
}
rep(i, N) {
int ans = frie.size(i) - 1 - count[i];
for (int e : v[i]) {
if (frie.same(i, e))
--ans;
}
cout << ans << " ";
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using llint = long long int;
#define rep(i, c) for (int i = 0; i < (int)c; ++i)
// c 回繰り返す
//////////////////////////クラス構造体///////////////////////////
class UnionFind { // UnionFind木 Nの数に注意 0=<x<N まで作られる
private:
vector<int> par;
public:
UnionFind(int N) : par(N, -1) {} // 初期化コンストラクタ 根を初期化
// par[i]は根のとき負 それ以外非負
int root(int x) { // データxの根を返す root(i)は必ず非負
if (par[x] < 0)
return x; // par[x]が負のときxは木の根である
return par[x] = root(par[x]); // 経路圧縮
}
void unite(int x, int y) { // xの根とyの根を同じにする
int rx = root(x);
int ry = root(y);
if (rx == ry)
return; // xの根とyの根が同じとき なにもしない
if (par[rx] > par[ry])
swap(rx, ry); // ランク処理
par[rx] += par[ry];
par[ry] = rx; // 違うとき 根をおなじにする
}
bool same(int x, int y) { // 根が同じとき TRUE
return root(x) == root(y);
}
int size(int x) { return -par[root(x)]; } // サイズを返す
};
///////////////////////////////////////////////////////////
int main() {
int N, M, K;
cin >> N >> M >> K;
UnionFind frie(N);
int count[100010];
vector<int> v[100010];
rep(i, M) {
int A, B;
cin >> A >> B;
--A;
--B;
count[A]++;
count[B]++;
frie.unite(A, B);
}
rep(i, K) {
int C, D;
cin >> C >> D;
--C;
--D;
v[C].push_back(D);
v[D].push_back(C);
}
rep(i, N) {
int ans = frie.size(i) - 1 - count[i];
for (int e : v[i]) {
if (frie.same(i, e))
--ans;
}
cout << ans << " ";
}
cout << endl;
return 0;
}
| replace | 45 | 46 | 45 | 46 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int deg[10005];
vector<int> to[10005];
int main() {
int n, m, k;
cin >> n >> m >> k;
UnionFind uf(n);
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
uf.unite(a, b);
}
rep(i, k) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
rep(i, n) {
int ans = uf.size(i) - 1 - deg[i];
for (int u : to[i]) {
if (uf.same(i, u))
ans--;
}
printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1) {}
int find(int x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
int deg[100005];
vector<int> to[100005];
int main() {
int n, m, k;
cin >> n >> m >> k;
UnionFind uf(n);
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
deg[a]++;
deg[b]++;
uf.unite(a, b);
}
rep(i, k) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
rep(i, n) {
int ans = uf.size(i) - 1 - deg[i];
for (int u : to[i]) {
if (uf.same(i, u))
ans--;
}
printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
}
return 0;
} | replace | 28 | 30 | 28 | 30 | 0 | |
p02762 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)(x).size();
inline int in() {
int x;
scanf("%d", &x);
return x;
}
vector<int> siz;
vector<int> par;
vector<set<int>> f;
vector<int> b;
int find(int node) {
while (node != par[node])
node = par[node];
return node;
}
void uni(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (siz[a] > siz[b])
swap(a, b);
siz[a] += siz[b];
par[b] = a;
}
int32_t main() {
int n = in();
int m = in();
int k = in();
siz = vector<int>(n, 1);
par = vector<int>(n);
b = siz;
f = vector<set<int>>(n);
iota(par.begin(), par.end(), 0);
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--;
v--;
uni(u, v);
f[u].insert(v);
f[v].insert(u);
}
for (int i = 0; i < k; i++) {
cin >> u >> v;
u--;
v--;
if (find(u) == find(v)) {
if (f[u].find(v) != f[u].end()) {
f[v].erase(u);
f[u].erase(v);
}
b[u]++;
b[v]++;
}
}
for (int i = 0; i < n; i++) {
cout << siz[find(i)] - (f[i].size() + b[i]) << " ";
}
} | #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)(x).size();
inline int in() {
int x;
scanf("%d", &x);
return x;
}
vector<int> siz;
vector<int> par;
vector<set<int>> f;
vector<int> b;
int find(int node) {
while (node != par[node])
node = par[node];
return node;
}
void uni(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
if (siz[a] < siz[b]) {
par[a] = b;
siz[b] += siz[a];
} else {
par[b] = a;
siz[a] += siz[b];
}
}
int32_t main() {
int n = in();
int m = in();
int k = in();
siz = vector<int>(n, 1);
par = vector<int>(n);
b = siz;
f = vector<set<int>>(n);
iota(par.begin(), par.end(), 0);
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--;
v--;
uni(u, v);
f[u].insert(v);
f[v].insert(u);
}
for (int i = 0; i < k; i++) {
cin >> u >> v;
u--;
v--;
if (find(u) == find(v)) {
if (f[u].find(v) != f[u].end()) {
f[v].erase(u);
f[u].erase(v);
}
b[u]++;
b[v]++;
}
}
for (int i = 0; i < n; i++) {
cout << siz[find(i)] - (f[i].size() + b[i]) << " ";
}
} | replace | 26 | 30 | 26 | 33 | TLE | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct UnionFind {
vector<int> par, rank;
UnionFind(int s) {
par.resize(s);
rank.resize(s, 0);
for (int i = 0; i < s; ++i)
par[i] = i;
}
int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
};
const int MAX_N = 100;
int Friend[MAX_N], Group[MAX_N];
int N, M, K;
int main() {
scanf("%d %d %d", &N, &M, &K);
UnionFind List(MAX_N);
for (int i = 0; i < M; ++i) {
int a, b;
scanf("%d %d", &a, &b);
--a, --b;
List.unite(a, b);
++Friend[a], ++Friend[b];
}
for (int i = 0; i < N; ++i) {
List.root(i);
}
for (int i = 0; i < N; ++i) {
++Group[List.par[i]];
}
for (int i = 0; i < N; ++i) {
Group[i] = Group[List.par[i]];
}
for (int i = 0; i < K; ++i) {
int c, d;
scanf("%d %d", &c, &d);
--c, --d;
if (List.same(c, d)) {
++Friend[c], ++Friend[d];
}
}
for (int i = 0; i < N; ++i) {
printf("%d ", Group[i] - 1 - Friend[i]);
}
printf("\n");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct UnionFind {
vector<int> par, rank;
UnionFind(int s) {
par.resize(s);
rank.resize(s, 0);
for (int i = 0; i < s; ++i)
par[i] = i;
}
int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y])
rank[x]++;
}
}
};
const int MAX_N = 100100;
int Friend[MAX_N], Group[MAX_N];
int N, M, K;
int main() {
scanf("%d %d %d", &N, &M, &K);
UnionFind List(MAX_N);
for (int i = 0; i < M; ++i) {
int a, b;
scanf("%d %d", &a, &b);
--a, --b;
List.unite(a, b);
++Friend[a], ++Friend[b];
}
for (int i = 0; i < N; ++i) {
List.root(i);
}
for (int i = 0; i < N; ++i) {
++Group[List.par[i]];
}
for (int i = 0; i < N; ++i) {
Group[i] = Group[List.par[i]];
}
for (int i = 0; i < K; ++i) {
int c, d;
scanf("%d %d", &c, &d);
--c, --d;
if (List.same(c, d)) {
++Friend[c], ++Friend[d];
}
}
for (int i = 0; i < N; ++i) {
printf("%d ", Group[i] - 1 - Friend[i]);
}
printf("\n");
return 0;
} | replace | 29 | 30 | 29 | 30 | 0 | |
p02762 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
int n;
vvi F; // n 頂点 m 辺のグラフ: 友達関係
vvi B; // n 頂点 k 辺のグラフ: ブロック関係
vi color; // グラフ F について同じ連結成分にいる頂点には同じ色をあてる
// vector<bool> seen;
void dfs(int v, int c) {
color[v] = c;
for (auto u : F[v]) {
if (color[u] != -1)
continue;
dfs(u, c);
}
}
void Main() {
int m, k;
cin >> n >> m >> k;
if (m == 0) {
rep(i, n) cout << 0 << " ";
return;
}
F.resize(n);
color.resize(n, -1);
// seen.resize(n, false);
rep(i, m) {
int a, b;
cin >> a >> b;
a--, b--;
F[a].push_back(b);
F[b].push_back(a);
}
// vi ans(n);
int c = 1;
rep(i, m) {
if (color[i] != -1)
continue;
dfs(i, c);
c++;
}
/*
rep(i,n) {
cout << color[i] << " ";
}
//*/
vi cnt(c, 0); // cnt[0] は使わない
rep(i, n) { cnt[color[i]]++; }
/*
rep(i,c){
cout << cnt[i] << " ";
}
//*/
/*
rep(i,n){
cout << cnt[color[i]] << " ";
}
//*/
B.resize(n);
rep(i, k) {
int a, b;
cin >> a >> b;
a--, b--;
if (color[a] == color[b]) {
B[a].push_back(b);
B[b].push_back(a);
}
}
/*
rep(i,n){
// for(auto u:B[i]) cout << i << " " << u << endl;
cout << B[i].size() << endl;
}
*/
rep(i, n) { cout << cnt[color[i]] - 1 - F[i].size() - B[i].size() << " "; }
cout << endl;
return;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
int n;
vvi F; // n 頂点 m 辺のグラフ: 友達関係
vvi B; // n 頂点 k 辺のグラフ: ブロック関係
vi color; // グラフ F について同じ連結成分にいる頂点には同じ色をあてる
// vector<bool> seen;
void dfs(int v, int c) {
color[v] = c;
for (auto u : F[v]) {
if (color[u] != -1)
continue;
dfs(u, c);
}
}
void Main() {
int m, k;
cin >> n >> m >> k;
if (m == 0) {
rep(i, n) cout << 0 << " ";
return;
}
F.resize(n);
color.resize(n, -1);
// seen.resize(n, false);
rep(i, m) {
int a, b;
cin >> a >> b;
a--, b--;
F[a].push_back(b);
F[b].push_back(a);
}
// vi ans(n);
int c = 1;
rep(i, n) {
if (color[i] != -1)
continue;
dfs(i, c);
c++;
}
/*
rep(i,n) {
cout << color[i] << " ";
}
//*/
vi cnt(c, 0); // cnt[0] は使わない
rep(i, n) { cnt[color[i]]++; }
/*
rep(i,c){
cout << cnt[i] << " ";
}
//*/
/*
rep(i,n){
cout << cnt[color[i]] << " ";
}
//*/
B.resize(n);
rep(i, k) {
int a, b;
cin >> a >> b;
a--, b--;
if (color[a] == color[b]) {
B[a].push_back(b);
B[b].push_back(a);
}
}
/*
rep(i,n){
// for(auto u:B[i]) cout << i << " " << u << endl;
cout << B[i].size() << endl;
}
*/
rep(i, n) { cout << cnt[color[i]] - 1 - F[i].size() - B[i].size() << " "; }
cout << endl;
return;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} | replace | 55 | 56 | 55 | 56 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
// UnionFind Template
template <class T> struct UnionFind { // 構造体の定義
vector<T> d; // メンバ変数
UnionFind(T n = 0)
: d(n, -1) {
} // コンストラクタ(構造体が呼ばれたときにすぐに実行される処理)
T find(T x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(T x, T y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] < d[y])
swap(x, y); // d[x] is smaller. Then, connect d[x] to d[y].
d[y] += d[x];
d[x] = y;
return true;
}
bool same(T x, T y) { return find(x) == find(y); }
T size(T x) { return -(d[find(x)]); }
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> fr(m);
vector<int> bl[100005];
UnionFind<int> uf(n);
rep(i, m) {
int a, b;
cin >> a >> b;
--a;
--b;
fr[a]++;
fr[b]++;
uf.unite(a, b);
}
rep(i, k) {
int a, b;
cin >> a >> b;
--a;
--b;
bl[a].push_back(b);
bl[b].push_back(a);
}
rep(i, n) {
int ans;
ans = uf.size(i) - 1 - fr[i];
for (int a : bl[i]) {
if (uf.same(i, a))
ans--;
}
cout << ans << " ";
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
// UnionFind Template
template <class T> struct UnionFind { // 構造体の定義
vector<T> d; // メンバ変数
UnionFind(T n = 0)
: d(n, -1) {
} // コンストラクタ(構造体が呼ばれたときにすぐに実行される処理)
T find(T x) {
if (d[x] < 0)
return x;
return d[x] = find(d[x]);
}
bool unite(T x, T y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] < d[y])
swap(x, y); // d[x] is smaller. Then, connect d[x] to d[y].
d[y] += d[x];
d[x] = y;
return true;
}
bool same(T x, T y) { return find(x) == find(y); }
T size(T x) { return -(d[find(x)]); }
};
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> fr(100005);
vector<int> bl[100005];
UnionFind<int> uf(n);
rep(i, m) {
int a, b;
cin >> a >> b;
--a;
--b;
fr[a]++;
fr[b]++;
uf.unite(a, b);
}
rep(i, k) {
int a, b;
cin >> a >> b;
--a;
--b;
bl[a].push_back(b);
bl[b].push_back(a);
}
rep(i, n) {
int ans;
ans = uf.size(i) - 1 - fr[i];
for (int a : bl[i]) {
if (uf.same(i, a))
ans--;
}
cout << ans << " ";
}
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll, ll>
vector<int> to[10010];
vector<int> group;
vector<int> block;
void dfs(int i, int gr, vector<int> &vec) {
if (group[i] != -1)
return;
vec[gr]++;
group[i] = gr;
for (auto x : to[i]) {
dfs(x, gr, vec);
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
group.resize(n, -1);
block.resize(n, 0);
vector<int> group_cnt;
int tmp = 0;
rep(i, n) {
if (group[i] == -1) {
group_cnt.push_back(0);
dfs(i, tmp, group_cnt);
tmp++;
}
}
rep(i, k) {
int c, d;
cin >> c >> d;
c--;
d--;
if (group[c] == group[d]) {
block[c]++;
block[d]++;
}
}
rep(i, n) {
cout << group_cnt[group[i]] - to[i].size() - block[i] - 1 << " ";
}
cout << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll, ll>
vector<int> to[100010];
vector<int> group;
vector<int> block;
void dfs(int i, int gr, vector<int> &vec) {
if (group[i] != -1)
return;
vec[gr]++;
group[i] = gr;
for (auto x : to[i]) {
dfs(x, gr, vec);
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
to[b].push_back(a);
}
group.resize(n, -1);
block.resize(n, 0);
vector<int> group_cnt;
int tmp = 0;
rep(i, n) {
if (group[i] == -1) {
group_cnt.push_back(0);
dfs(i, tmp, group_cnt);
tmp++;
}
}
rep(i, k) {
int c, d;
cin >> c >> d;
c--;
d--;
if (group[c] == group[d]) {
block[c]++;
block[d]++;
}
}
rep(i, n) {
cout << group_cnt[group[i]] - to[i].size() - block[i] - 1 << " ";
}
cout << endl;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02762 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using llong = long long;
//===
struct UnionFind {
int n;
vector<int> parent;
UnionFind() {}
UnionFind(int nmemb) { init(nmemb); };
void init(int nmemb) {
parent.clear();
parent.assign(nmemb, -1);
};
int root(int x) {
if (parent[x] < 0) {
return x;
}
return parent[x] = root(parent[x]);
};
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (parent[y] < parent[x])
swap(x, y);
parent[x] += parent[y];
parent[y] = x;
return;
};
bool same(int x, int y) { return root(x) == root(y); };
int size(int x) { return -(parent[root(x)]); };
};
//===
UnionFind fore(100005);
vector<vector<int>> block(100005);
vector<vector<int>> fg(10005);
llong n, m, k;
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
llong a, b;
cin >> a >> b;
fore.unite(a, b);
fg[a].push_back(b);
fg[b].push_back(a);
}
for (int i = 0; i < k; i++) {
llong a, b;
cin >> a >> b;
block[a].push_back(b);
block[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
llong ans = fore.size(i);
ans -= fg[i].size();
for (auto a : block[i]) {
if (fore.same(a, i)) {
ans--;
}
}
ans--;
cout << ans << ' ';
}
cout << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using llong = long long;
//===
struct UnionFind {
int n;
vector<int> parent;
UnionFind() {}
UnionFind(int nmemb) { init(nmemb); };
void init(int nmemb) {
parent.clear();
parent.assign(nmemb, -1);
};
int root(int x) {
if (parent[x] < 0) {
return x;
}
return parent[x] = root(parent[x]);
};
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return;
if (parent[y] < parent[x])
swap(x, y);
parent[x] += parent[y];
parent[y] = x;
return;
};
bool same(int x, int y) { return root(x) == root(y); };
int size(int x) { return -(parent[root(x)]); };
};
//===
UnionFind fore(100005);
vector<vector<int>> block(100005);
vector<vector<int>> fg(100005);
llong n, m, k;
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
llong a, b;
cin >> a >> b;
fore.unite(a, b);
fg[a].push_back(b);
fg[b].push_back(a);
}
for (int i = 0; i < k; i++) {
llong a, b;
cin >> a >> b;
block[a].push_back(b);
block[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
llong ans = fore.size(i);
ans -= fg[i].size();
for (auto a : block[i]) {
if (fore.same(a, i)) {
ans--;
}
}
ans--;
cout << ans << ' ';
}
cout << endl;
return 0;
}
| replace | 59 | 60 | 59 | 60 | 0 | |
p02762 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool find(int x, int y) { return root(x) == root(y); }
int size(int x) { return -data[root(x)]; }
};
int main() {
int n, m, k;
cin >> n >> m >> k;
UnionFind uf(n);
vector<int> a(m), b(m), c(m), d(m);
rep(i, m) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
uf.unite(a[i], b[i]);
}
rep(i, k) {
cin >> c[i] >> d[i];
c[i]--;
d[i]--;
}
vector<int> cntfr(n), cntbl(n);
rep(i, m) {
cntfr[a[i]]++;
cntfr[b[i]]++;
}
rep(i, k) {
if (uf.find(c[i], d[i])) {
cntbl[c[i]]++;
cntbl[d[i]]++;
}
}
rep(i, n) {
cout << uf.size(i) - 1 - cntfr[i] - cntbl[i] << " \n"[i == n - 1];
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
bool find(int x, int y) { return root(x) == root(y); }
int size(int x) { return -data[root(x)]; }
};
int main() {
int n, m, k;
cin >> n >> m >> k;
UnionFind uf(n);
vector<int> a(m), b(m), c(k), d(k);
rep(i, m) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
uf.unite(a[i], b[i]);
}
rep(i, k) {
cin >> c[i] >> d[i];
c[i]--;
d[i]--;
}
vector<int> cntfr(n), cntbl(n);
rep(i, m) {
cntfr[a[i]]++;
cntfr[b[i]]++;
}
rep(i, k) {
if (uf.find(c[i], d[i])) {
cntbl[c[i]]++;
cntbl[d[i]]++;
}
}
rep(i, n) {
cout << uf.size(i) - 1 - cntfr[i] - cntbl[i] << " \n"[i == n - 1];
}
}
| replace | 53 | 54 | 53 | 54 | 0 | |
p02762 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define I_MAX 2147483647
#define LL_MAX 9223372036854775807
#define ll long long
#define ld long double
struct XX {
string s;
int p;
int c1;
int c2;
int num;
};
class xxGreater {
public:
bool operator()(const XX &riLeft, const XX &riRight) const {
// 第2条件
if ((riLeft.s) == (riRight.s)) {
return riLeft.p <
riRight.p; //<:昇順(小さいものから順番)、>:降順(大きいものから順番)
// プライオリティキューの場合は > で、top()すると値の小さいものがとれる
}
// 第1条件
return (riLeft.s) < (riRight.s);
}
};
// map<long long,long long> prime_f(long long n){
// map<long long,long long>res;
// for(int i=2;i*i<=n;i++){
// while(n%i==0){
// ++res[i];
// n/=i;
// }
// }
// if(n!=1)res[n]=1;
// return res;
// }
// int n;
////int dat[2*10000000];
////int dat2[2*10000000];
// int dat[10];
// int dat2[10];
//
// void init(int n_){
// n=1;
// while(n<n_)n*=2;
// for(int i=0;i<2*n-1;i++){
// dat[i]=0;
// dat2[i]=0;
// }
// }
//
// void initset(int k,int a){
// k+=n-1;
// dat[k]=a;
// while(k>0){
// k=(k-1)/2;
// dat[k]=dat[k*2+1]+dat[k*2+2];
// }
// }
//
////[a,b)の間を[l,r]区間で比較しアップデート
////引数のindexに注意
////nは固定。initで計算すみ
////update2(L[i],R[i]+1,0,0,n,D[i]);
// void update2(int a,int b,int k,int l,int r,int v){//v更新値、区間は0-index
// if(r<=a || b<=l)return;
// if(a<=l && r<=b){
// dat[k]+=dat2[k];
// if(r-l>1){
// dat2[k*2+1]+=dat2[k]/2;
// dat2[k*2+1]+=dat2[k]/2;
// }
// dat2[k]=v*(r-l);
// return;
// }else{
// update2(a,b,k*2+1,l,(l+r)/2,v);
// update2(a,b,k*2+2,(l+r)/2,r,v);
// return;
// }
// }
//
// int query(int a,int b,int k,int l,int r){
// if(r<=a || b<=l)return 0;
// if(a<=l && r<=b){
// dat[k]+=dat2[k];
// if(r-l>1){
// dat2[k*2+1]+=dat2[k]/2;
// dat2[k*2+1]+=dat2[k]/2;
// }
// dat2[k]=0;
// return dat[k];
// }
// else{
// int vl=query(a,b,k*2+1,l,(l+r)/2);
// int vr=query(a,b,k*2+2,(l+r)/2,r);
// return vl+vr;
// }
// }
// void printb(unsigned int v) {
// unsigned int mask = (int)1 << (sizeof(v) * CHAR_BIT - 1);
// do putchar(mask & v ? '1' : '0');
// while (mask >>= 1);
// }
#ifdef DEBUG
#else
#endif
set<int> G1[10001];
set<int> G2[10001];
ll oya[10001];
ll nak[10001];
ll non[10001];
int ppar[100001]; // 親(1-index)
int rrank[100001]; // 木の深さ
void init(int n) { //
for (int i = 1; i <= n; i++) {
ppar[i] = i;
rrank[i] = 0;
}
}
int find(int x) {
if (ppar[x] == x) {
return x;
} else {
return ppar[x] = find(ppar[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (rrank[x] < rrank[y]) {
ppar[x] = y;
} else {
ppar[y] = x;
if (rrank[x] == rrank[y]) {
rrank[x]++;
}
}
}
bool same(int x, int y) { return find(x) == find(y); }
int main(int argc, const char *argv[]) {
// scanf("%s",S);
// scanf("%d",&N);
// scanf("%lld %lld",&target1,&target2);
// sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]);
// getline(cin, target);
// ifstream ifs("01");//テスト用
// ifs >> a;
// ここから
// 入力高速化
ios::sync_with_stdio(false);
cin.tie(0);
int N, M, K;
cin >> N >> M >> K;
init(N);
for (int i = 0; i < M; i++) {
int t1, t2;
cin >> t1 >> t2;
G1[t1].insert(t2);
G1[t2].insert(t1);
unite(t1, t2);
}
for (int i = 0; i < K; i++) {
int t1, t2;
cin >> t1 >> t2;
if (same(t1, t2)) {
G2[t1].insert(t2);
G2[t2].insert(t1);
}
}
for (int i = 1; i <= N; i++) {
int t = find(i);
oya[t]++;
}
for (int i = 1; i <= N; i++) {
int ans = 0;
int t = find(i);
ans += oya[t] - 1;
ans -= G1[i].size();
ans -= G2[i].size();
cout << ans << " ";
}
cout << endl;
// ここまで
// cout << "ans" << endl;
// cout << " " << "ans" << endl;
// printf("%.0f\n",ans);//小数点以下表示なし
// printf("%.7f\n",p);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define I_MAX 2147483647
#define LL_MAX 9223372036854775807
#define ll long long
#define ld long double
struct XX {
string s;
int p;
int c1;
int c2;
int num;
};
class xxGreater {
public:
bool operator()(const XX &riLeft, const XX &riRight) const {
// 第2条件
if ((riLeft.s) == (riRight.s)) {
return riLeft.p <
riRight.p; //<:昇順(小さいものから順番)、>:降順(大きいものから順番)
// プライオリティキューの場合は > で、top()すると値の小さいものがとれる
}
// 第1条件
return (riLeft.s) < (riRight.s);
}
};
// map<long long,long long> prime_f(long long n){
// map<long long,long long>res;
// for(int i=2;i*i<=n;i++){
// while(n%i==0){
// ++res[i];
// n/=i;
// }
// }
// if(n!=1)res[n]=1;
// return res;
// }
// int n;
////int dat[2*10000000];
////int dat2[2*10000000];
// int dat[10];
// int dat2[10];
//
// void init(int n_){
// n=1;
// while(n<n_)n*=2;
// for(int i=0;i<2*n-1;i++){
// dat[i]=0;
// dat2[i]=0;
// }
// }
//
// void initset(int k,int a){
// k+=n-1;
// dat[k]=a;
// while(k>0){
// k=(k-1)/2;
// dat[k]=dat[k*2+1]+dat[k*2+2];
// }
// }
//
////[a,b)の間を[l,r]区間で比較しアップデート
////引数のindexに注意
////nは固定。initで計算すみ
////update2(L[i],R[i]+1,0,0,n,D[i]);
// void update2(int a,int b,int k,int l,int r,int v){//v更新値、区間は0-index
// if(r<=a || b<=l)return;
// if(a<=l && r<=b){
// dat[k]+=dat2[k];
// if(r-l>1){
// dat2[k*2+1]+=dat2[k]/2;
// dat2[k*2+1]+=dat2[k]/2;
// }
// dat2[k]=v*(r-l);
// return;
// }else{
// update2(a,b,k*2+1,l,(l+r)/2,v);
// update2(a,b,k*2+2,(l+r)/2,r,v);
// return;
// }
// }
//
// int query(int a,int b,int k,int l,int r){
// if(r<=a || b<=l)return 0;
// if(a<=l && r<=b){
// dat[k]+=dat2[k];
// if(r-l>1){
// dat2[k*2+1]+=dat2[k]/2;
// dat2[k*2+1]+=dat2[k]/2;
// }
// dat2[k]=0;
// return dat[k];
// }
// else{
// int vl=query(a,b,k*2+1,l,(l+r)/2);
// int vr=query(a,b,k*2+2,(l+r)/2,r);
// return vl+vr;
// }
// }
// void printb(unsigned int v) {
// unsigned int mask = (int)1 << (sizeof(v) * CHAR_BIT - 1);
// do putchar(mask & v ? '1' : '0');
// while (mask >>= 1);
// }
#ifdef DEBUG
#else
#endif
set<int> G1[100001];
set<int> G2[100001];
ll oya[100001];
ll nak[100001];
ll non[100001];
int ppar[100001]; // 親(1-index)
int rrank[100001]; // 木の深さ
void init(int n) { //
for (int i = 1; i <= n; i++) {
ppar[i] = i;
rrank[i] = 0;
}
}
int find(int x) {
if (ppar[x] == x) {
return x;
} else {
return ppar[x] = find(ppar[x]);
}
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (rrank[x] < rrank[y]) {
ppar[x] = y;
} else {
ppar[y] = x;
if (rrank[x] == rrank[y]) {
rrank[x]++;
}
}
}
bool same(int x, int y) { return find(x) == find(y); }
int main(int argc, const char *argv[]) {
// scanf("%s",S);
// scanf("%d",&N);
// scanf("%lld %lld",&target1,&target2);
// sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]);
// getline(cin, target);
// ifstream ifs("01");//テスト用
// ifs >> a;
// ここから
// 入力高速化
ios::sync_with_stdio(false);
cin.tie(0);
int N, M, K;
cin >> N >> M >> K;
init(N);
for (int i = 0; i < M; i++) {
int t1, t2;
cin >> t1 >> t2;
G1[t1].insert(t2);
G1[t2].insert(t1);
unite(t1, t2);
}
for (int i = 0; i < K; i++) {
int t1, t2;
cin >> t1 >> t2;
if (same(t1, t2)) {
G2[t1].insert(t2);
G2[t2].insert(t1);
}
}
for (int i = 1; i <= N; i++) {
int t = find(i);
oya[t]++;
}
for (int i = 1; i <= N; i++) {
int ans = 0;
int t = find(i);
ans += oya[t] - 1;
ans -= G1[i].size();
ans -= G2[i].size();
cout << ans << " ";
}
cout << endl;
// ここまで
// cout << "ans" << endl;
// cout << " " << "ans" << endl;
// printf("%.0f\n",ans);//小数点以下表示なし
// printf("%.7f\n",p);
return 0;
}
| replace | 132 | 137 | 132 | 137 | 0 | |
p02762 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
template <typename T> inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = x * 10 + (ch ^ 48), ch = getchar();
x *= f;
return;
}
template <typename T> void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
write(x / 10);
putchar(x % 10 + '0');
return;
}
const int MAXN = 100010;
int n, m, k;
struct node {
int tot = 1;
int edge[MAXN * 2], nxt[MAXN * 2], hd[MAXN];
inline void add_edge(int u, int v) {
edge[tot] = v, nxt[tot] = hd[u], hd[u] = tot++;
}
} g1, g2;
int f[MAXN];
int find(int x) {
if (f[x] == x)
return f[x];
return find(f[x]);
}
inline void Union(int u, int v) {
u = find(u), v = find(v);
if (u != v)
f[u] = v;
}
bool book[MAXN];
vector<int> d[MAXN];
int sum[MAXN];
int main() {
read(n), read(m), read(k);
for (register int i = 1; i <= n; i++)
f[i] = i;
for (register int i = 1; i <= m; i++) {
int u, v;
read(u), read(v);
g1.add_edge(u, v), g1.add_edge(v, u);
Union(u, v);
}
for (register int i = 1; i <= k; i++) {
int u, v;
read(u), read(v);
g2.add_edge(u, v), g2.add_edge(v, u);
}
for (register int i = 1; i <= n; i++)
d[find(i)].push_back(i);
for (register int i = 1; i <= n; i++) {
int tot = 0;
for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
book[*it] = 1, tot++;
for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++) {
int x = *it;
int cnt = 0;
for (int j = g1.hd[x]; j; j = g1.nxt[j])
if (book[g1.edge[j]])
cnt++;
for (int j = g2.hd[x]; j; j = g2.nxt[j])
if (book[g2.edge[j]])
cnt++;
sum[x] = tot - cnt - 1;
}
for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
book[*it] = 0;
}
for (register int i = 1; i <= n; i++)
write(sum[i]), putchar(' ');
putchar('\n');
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
template <typename T> inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = x * 10 + (ch ^ 48), ch = getchar();
x *= f;
return;
}
template <typename T> void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
write(x / 10);
putchar(x % 10 + '0');
return;
}
const int MAXN = 100010;
int n, m, k;
struct node {
int tot = 1;
int edge[MAXN * 2], nxt[MAXN * 2], hd[MAXN];
inline void add_edge(int u, int v) {
edge[tot] = v, nxt[tot] = hd[u], hd[u] = tot++;
}
} g1, g2;
int f[MAXN];
int find(int x) {
if (f[x] == x)
return f[x];
return f[x] = find(f[x]);
}
inline void Union(int u, int v) {
u = find(u), v = find(v);
if (u != v)
f[u] = v;
}
bool book[MAXN];
vector<int> d[MAXN];
int sum[MAXN];
int main() {
read(n), read(m), read(k);
for (register int i = 1; i <= n; i++)
f[i] = i;
for (register int i = 1; i <= m; i++) {
int u, v;
read(u), read(v);
g1.add_edge(u, v), g1.add_edge(v, u);
Union(u, v);
}
for (register int i = 1; i <= k; i++) {
int u, v;
read(u), read(v);
g2.add_edge(u, v), g2.add_edge(v, u);
}
for (register int i = 1; i <= n; i++)
d[find(i)].push_back(i);
for (register int i = 1; i <= n; i++) {
int tot = 0;
for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
book[*it] = 1, tot++;
for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++) {
int x = *it;
int cnt = 0;
for (int j = g1.hd[x]; j; j = g1.nxt[j])
if (book[g1.edge[j]])
cnt++;
for (int j = g2.hd[x]; j; j = g2.nxt[j])
if (book[g2.edge[j]])
cnt++;
sum[x] = tot - cnt - 1;
}
for (vector<int>::iterator it = d[i].begin(); it != d[i].end(); it++)
book[*it] = 0;
}
for (register int i = 1; i <= n; i++)
write(sum[i]), putchar(' ');
putchar('\n');
return 0;
}
| replace | 38 | 39 | 38 | 39 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.