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
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <climits> #include <cmath> #include <iomanip> #include <math.h> #include <type_traits> #include <unordered_map> #include <unordered_set> // #define local #ifdef local #include "dbg-macro/dbg.h" #endif // #define hacks #ifdef hacks #include <boost/multiprecision/cpp_int.hpp> #endif #define p std::pair #define ll long long #define ld long double #define ull unsigned long long #define pi std::pair<int, int> #define stdabs std::abs #define all(x) (x).begin(), (x).end() #define rep(i, n) \ for (unsigned long long i = 0; i < (unsigned long long)(n); ++i) #define vec std::vector #define oreq |= #define npm next_permutation using namespace std; std::vector<unsigned ll> genprimevec(const unsigned ll N); ll extgcd(ll a, ll b, ll &x, ll &y); ll nCr(ll n, ll r); void stat(); template <typename T> T fact(T num); constexpr ll mod = 1000000000 + 7; int main() { stat(); ll n; cin >> n; ull ans = 0; for (ll i = 0; i != n; ++i) { ans += i % (i + 1); } cout << ans << endl; return 0; } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } std::vector<unsigned ll> genprimevec(const unsigned ll N) { std::vector<bool> is_prime(N + 1); for (unsigned ll i = 0; i <= N; i++) { is_prime[i] = true; } std::vector<unsigned ll> P; for (unsigned ll i = 2; i <= N; i++) { if (is_prime[i]) { for (unsigned ll j = 2 * i; j <= N; j += i) { is_prime[j] = false; } P.emplace_back(i); } } return P; } void stat(){ #ifdef local rep(i, 2){std::cout << "local enable" << std::endl; } #endif #ifdef hacks rep(i, 2) { std::cout << "boost enable" << std::endl; } #endif } ll nCr(ll n, ll r) { ll num = 1; for (ll i = 1; i <= r; i++) { num = num * (n - i + 1) / i; } return num; } template <typename T> T fact(T num) { if (num == 1) { return 1; } return num * fact(num - 1); }
#include <bits/stdc++.h> #include <climits> #include <cmath> #include <iomanip> #include <math.h> #include <type_traits> #include <unordered_map> #include <unordered_set> // #define local #ifdef local #include "dbg-macro/dbg.h" #endif // #define hacks #ifdef hacks #include <boost/multiprecision/cpp_int.hpp> #endif #define p std::pair #define ll long long #define ld long double #define ull unsigned long long #define pi std::pair<int, int> #define stdabs std::abs #define all(x) (x).begin(), (x).end() #define rep(i, n) \ for (unsigned long long i = 0; i < (unsigned long long)(n); ++i) #define vec std::vector #define oreq |= #define npm next_permutation using namespace std; std::vector<unsigned ll> genprimevec(const unsigned ll N); ll extgcd(ll a, ll b, ll &x, ll &y); ll nCr(ll n, ll r); void stat(); template <typename T> T fact(T num); constexpr ll mod = 1000000000 + 7; int main() { stat(); ll n; cin >> n; cout << n * (n - 1) / 2 << endl; return 0; } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } std::vector<unsigned ll> genprimevec(const unsigned ll N) { std::vector<bool> is_prime(N + 1); for (unsigned ll i = 0; i <= N; i++) { is_prime[i] = true; } std::vector<unsigned ll> P; for (unsigned ll i = 2; i <= N; i++) { if (is_prime[i]) { for (unsigned ll j = 2 * i; j <= N; j += i) { is_prime[j] = false; } P.emplace_back(i); } } return P; } void stat(){ #ifdef local rep(i, 2){std::cout << "local enable" << std::endl; } #endif #ifdef hacks rep(i, 2) { std::cout << "boost enable" << std::endl; } #endif } ll nCr(ll n, ll r) { ll num = 1; for (ll i = 1; i <= r; i++) { num = num * (n - i + 1) / i; } return num; } template <typename T> T fact(T num) { if (num == 1) { return 1; } return num * fact(num - 1); }
replace
39
44
39
40
TLE
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const double pi = 3.14159265358979323846; #define rep(i, n) for (ll i = 0; i < n; i++) #define mod 1000000007LL #define PS(p) cout << setprecision(15) << fixed << p << endl; #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); #define ALL(c) c.begin(), c.end() #define CIN(v) cin >> v; #define COUT(v) cout << v << endl; int main() { ll N; CIN(N); ll c = 0; ll m = 0; ll t = 0; for (ll i = 1; i < N; i++) { t = (i) % (N + i); m += t; // COUT(m << " t " << t << " i " << i + 1 << " n " << N - i); } COUT(m); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const double pi = 3.14159265358979323846; #define rep(i, n) for (ll i = 0; i < n; i++) #define mod 1000000007LL #define PS(p) cout << setprecision(15) << fixed << p << endl; #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); #define ALL(c) c.begin(), c.end() #define CIN(v) cin >> v; #define COUT(v) cout << v << endl; int main() { ll N; CIN(N); COUT(N * (N - 1) / 2); return 0; }
replace
20
30
20
21
TLE
p02924
C++
Runtime Error
#include <algorithm> #include <array> #include <cmath> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; namespace io { // Input using IS = istream; IS &in(IS &i) { return i; } template <class T, class... Ts> IS &in(IS &i, T &a, Ts &...as) { return in(i >> a, as...); } template <class V> IS &operator>>(IS &i, vector<V> &a) { for_each(a.begin(), a.end(), [&i](auto &e) { i >> e; }); return i; } template <class F, class S> IS &operator>>(IS &i, pair<F, S> const &p) { return in(p.first, p.second); } template <class... Ts, size_t... I> IS &tuple_in(IS &i, tuple<Ts...> &t, index_sequence<I...>) { return in(i, get<I>(t)...); } template <class... Ts> IS &operator>>(IS &i, tuple<Ts...> &t) { return tuple_in(i, t, make_index_sequence<sizeof...(Ts)>()); } // Output using OS = ostream; OS &out(OS &o) { return o << endl; } template <class T> OS &out(OS &o, T const &a) { return o << a << endl; } template <class T, class... Ts> OS &out(OS &o, T const &a, Ts const &...as) { return out(o << a << " ", as...); } template <class F, class S> OS &operator<<(OS &o, pair<F, S> const &p) { return o << p.first << ":" << p.second; } template <class... Ts> OS &args_out(OS &o, Ts const &...ts); OS &args_out(OS &o) { return o; } template <class T> OS &args_out(OS &o, T const &t) { return o << t; } template <class T0, class T1, class... Ts> OS &args_out(OS &o, T0 const &t0, T1 const &t1, Ts const &...ts) { return args_out(o << t0 << ",", t1, ts...); } template <class... Ts, size_t... I> OS &tuple_out(OS &o, tuple<Ts...> const &t, index_sequence<I...>) { return args_out(o, get<I>(t)...); } template <class... Ts> OS &operator<<(OS &o, tuple<Ts...> const &t) { return tuple_out(o << "(", t, make_index_sequence<sizeof...(Ts)>()) << ")"; } template < class C, class T = typename iterator_traits<typename C::iterator>::value_type, typename enable_if<!is_same<C, string>::value, nullptr_t>::type = nullptr> OS &operator<<(OS &o, C const &a) { return a.empty() ? (o << "[]") : ([&o, &a]() -> OS & { o << "[" << *a.begin(); for_each(next(a.begin()), a.end(), [&o](auto const &e) { o << "," << e; }); return o << "]"; }()); } } // namespace io template <class... Xs> void input(Xs &...xs) { io::in(cin, xs...); } template <class... Xs> void print(Xs const &...xs) { io::out(cout, xs...); } #ifdef JUMPAKU_DEBUG template <class... Xs> void dump(Xs const &...xs) { io::out(cout, xs...); } #else template <class... Xs> void dump(Xs const &...xs) {} #endif // Hash namespace hashcode { template <class... Ts> size_t hash_args(size_t h, Ts const &...ts); size_t hash_args(size_t h) { return h; } template <class T, class... Ts> size_t hash_args(size_t h, T const &t, Ts const &...ts) { return hash_args(h * 31 + hash<T>{}(t), ts...); } template <class... Ts, size_t... I> size_t hash_tuple(tuple<Ts...> const &t, index_sequence<I...>) { return hash_args(17, get<I>(t)...); } } // namespace hashcode namespace std { template <class... Ts> struct hash<tuple<Ts...>> { size_t operator()(tuple<Ts...> const &t) const { return hashcode::hash_tuple(t, index_sequence_for<Ts...>()); } }; } // namespace std // Types using ll = long long int; template <class T> using u_set = unordered_set<T>; template <class K, class V> using u_map = unordered_map<K, V>; // Range vector<ll> range(ll const &begin, ll const &end) { vector<ll> ret(max(0LL, end - begin)); iota(ret.begin(), ret.end(), begin); return ret; } vector<ll> range(ll const &end) { return range(0, end); } template <class T = ll> vector<T> vec(size_t n, T &&init = T()) { return vector<T>(n, init); } template <class Itr, class T = typename iterator_traits<Itr>::value_type> vector<T> vec(Itr begin, Itr end) { return vector<T>(begin, end); } // MOD ll gcd(ll p, ll q) { return (q == 0) ? p : gcd(q, p % q); } ll pow(ll a, ll n, ll m) { if (n == 0) return 1; if (n & 1) return ((a % m) * pow(a, n - 1, m)) % m; auto b = pow(a, n / 2, m); return (b * b) % m; } ll inv(ll a, ll p) { return pow(a, p - 2, p); } constexpr ll MOD = 1e9 + 7; int main() { ll N; input(N); ll ans = 0; for (auto i : range(2, N + 1)) { ans += (i - 1) % i; } print(ans); }
#include <algorithm> #include <array> #include <cmath> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; namespace io { // Input using IS = istream; IS &in(IS &i) { return i; } template <class T, class... Ts> IS &in(IS &i, T &a, Ts &...as) { return in(i >> a, as...); } template <class V> IS &operator>>(IS &i, vector<V> &a) { for_each(a.begin(), a.end(), [&i](auto &e) { i >> e; }); return i; } template <class F, class S> IS &operator>>(IS &i, pair<F, S> const &p) { return in(p.first, p.second); } template <class... Ts, size_t... I> IS &tuple_in(IS &i, tuple<Ts...> &t, index_sequence<I...>) { return in(i, get<I>(t)...); } template <class... Ts> IS &operator>>(IS &i, tuple<Ts...> &t) { return tuple_in(i, t, make_index_sequence<sizeof...(Ts)>()); } // Output using OS = ostream; OS &out(OS &o) { return o << endl; } template <class T> OS &out(OS &o, T const &a) { return o << a << endl; } template <class T, class... Ts> OS &out(OS &o, T const &a, Ts const &...as) { return out(o << a << " ", as...); } template <class F, class S> OS &operator<<(OS &o, pair<F, S> const &p) { return o << p.first << ":" << p.second; } template <class... Ts> OS &args_out(OS &o, Ts const &...ts); OS &args_out(OS &o) { return o; } template <class T> OS &args_out(OS &o, T const &t) { return o << t; } template <class T0, class T1, class... Ts> OS &args_out(OS &o, T0 const &t0, T1 const &t1, Ts const &...ts) { return args_out(o << t0 << ",", t1, ts...); } template <class... Ts, size_t... I> OS &tuple_out(OS &o, tuple<Ts...> const &t, index_sequence<I...>) { return args_out(o, get<I>(t)...); } template <class... Ts> OS &operator<<(OS &o, tuple<Ts...> const &t) { return tuple_out(o << "(", t, make_index_sequence<sizeof...(Ts)>()) << ")"; } template < class C, class T = typename iterator_traits<typename C::iterator>::value_type, typename enable_if<!is_same<C, string>::value, nullptr_t>::type = nullptr> OS &operator<<(OS &o, C const &a) { return a.empty() ? (o << "[]") : ([&o, &a]() -> OS & { o << "[" << *a.begin(); for_each(next(a.begin()), a.end(), [&o](auto const &e) { o << "," << e; }); return o << "]"; }()); } } // namespace io template <class... Xs> void input(Xs &...xs) { io::in(cin, xs...); } template <class... Xs> void print(Xs const &...xs) { io::out(cout, xs...); } #ifdef JUMPAKU_DEBUG template <class... Xs> void dump(Xs const &...xs) { io::out(cout, xs...); } #else template <class... Xs> void dump(Xs const &...xs) {} #endif // Hash namespace hashcode { template <class... Ts> size_t hash_args(size_t h, Ts const &...ts); size_t hash_args(size_t h) { return h; } template <class T, class... Ts> size_t hash_args(size_t h, T const &t, Ts const &...ts) { return hash_args(h * 31 + hash<T>{}(t), ts...); } template <class... Ts, size_t... I> size_t hash_tuple(tuple<Ts...> const &t, index_sequence<I...>) { return hash_args(17, get<I>(t)...); } } // namespace hashcode namespace std { template <class... Ts> struct hash<tuple<Ts...>> { size_t operator()(tuple<Ts...> const &t) const { return hashcode::hash_tuple(t, index_sequence_for<Ts...>()); } }; } // namespace std // Types using ll = long long int; template <class T> using u_set = unordered_set<T>; template <class K, class V> using u_map = unordered_map<K, V>; // Range vector<ll> range(ll const &begin, ll const &end) { vector<ll> ret(max(0LL, end - begin)); iota(ret.begin(), ret.end(), begin); return ret; } vector<ll> range(ll const &end) { return range(0, end); } template <class T = ll> vector<T> vec(size_t n, T &&init = T()) { return vector<T>(n, init); } template <class Itr, class T = typename iterator_traits<Itr>::value_type> vector<T> vec(Itr begin, Itr end) { return vector<T>(begin, end); } // MOD ll gcd(ll p, ll q) { return (q == 0) ? p : gcd(q, p % q); } ll pow(ll a, ll n, ll m) { if (n == 0) return 1; if (n & 1) return ((a % m) * pow(a, n - 1, m)) % m; auto b = pow(a, n / 2, m); return (b * b) % m; } ll inv(ll a, ll p) { return pow(a, p - 2, p); } constexpr ll MOD = 1e9 + 7; int main() { ll N; input(N); print(N * (N - 1) / 2); }
replace
141
146
141
142
0
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, sum = 0, tmp; cin >> n; for (int i = 1; i < n; i++) { tmp = i % (i + 1); sum += tmp; } cout << sum << '\n'; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, sum = 0, tmp; cin >> n; for (int i = 1; i < n; i++) { sum += i; } cout << sum << '\n'; }
replace
9
11
9
10
TLE
p02924
C++
Time Limit Exceeded
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; const double PI = acos(-1); int main() { long n; cin >> n; long ans = 0; for (int i = 1; i <= n; i++) { if (i != n) ans += i % (i + 1); } cout << ans << endl; return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <set> #include <stdio.h> #include <string> #include <vector> using namespace std; const double PI = acos(-1); int main() { long n; cin >> n; long ans = n * (n - 1) / 2; cout << ans << endl; return 0; }
replace
18
23
18
19
TLE
p02924
C++
Runtime Error
/* vrrtll==???>;::::~~~~~~......`.`````````````````````...-?777!_.._?7u,~~~::::;>>??=lllttrrzu rtll==??>>;::::~~~~......`.`````````````````` ..J77!`````````...`..._T,~~~~:::;;>??==lttrrv tll=??>>;:::~~~~......``````````````````..J7^ ` `` ` `` .,```````...._4,.~~~::;;>>?===lttr l=???>;;::~~~......`````HTSu,.`` `..J7! ` `` .J"~N```````````..?&.~~~~::;;>>?==llt =??>;;::~~~~.....``````.@????7SJ.?= ` ` `` .J=....J; ``````````..h..~~~~::;;>??=ll ?>>;::~~~~.....````````.D?>>?>?>?8+.`` `.J"_......_b` ````````.S_.~~~~::;;>??=l >;;::~~~~....``````````.C>??>>>?>>>?8J.` ```..Y~..........J; ` ` ``````` G...~~~~::;>??= ;;::~~~....```````` `..W1>>?>>>>?>>>>>?S,.`.7^.............-N`` ` ``````` 6...~~~~::;>?? ;:~~~~....``````` ..7` d??>>?>?>>?>>?>>1udMgggNNNNggJ.......([ `````.L...~~~~::;>? :~~~.....`````` .7` `K>?>?>>>>>>+ugNMMMB""7<!~~<?7TWMNNa,..w.` ` ` ` `````,)....~~:::;> ~~~....``````.J^ ` #>>?>>>?jgMMM9=_................-?TMMa,b` ` ` ` ````(,...~~~~:;; ~~~....``` .7`` ` @?>>?1uMM#=.........................(TMNa...... ` ``````4....~~~::; ~~~...`` .=`` ` ` .b>>jgNH".................`.............?HNmx??17TYY9SA+(..L....~~~:: ~....` ,^`` ` ` .b+dN#^............6..-(,-...`............(HMm+>>>>>?>>????zOM_.~~~:: .... .=``` `` ` ...JNMM^..........`..._n.(MMN,....`..`.........?MNe<>>?>??>????d^...~~~: ~...v```` ` ..-Z""!_..(M@_........`........?7MMMMp.................-TNN+?>>>????1d4-..-(Jk9 ..(^`...zY"!_........(MD..............`......JMMMMp....`..`..`......./MNx>??>>?1d!.h7=~(??= (v_`,R_.............(NF..(/..(&,...`..........?MMMM;..................(MMs>>?1&T"!``....(1= t..`` 4,...........(N@....?,(NMNR_.....`..`....(WMM$..`....`..`..`....._HMe7=```````....~_1 ...````.4,........-dM:.....(7MMMNR-.....................`............(.?^ `` ``````....~~~ ...``````,4,....`.(NF........(MMMMb..`....--................`....(.7! ` ````....~~: ..````` ` `.5J_...JMt.........?NMMM[...`.HH5._(J7[...`...`...--?^` ` `````....~~~: ...````` ` ` 7a,.dM{....`...../MMMN......(J=` .>......._(/= ` ` `````...~~~: ....```` `` (4MN{..........._7"^...(/^ `.C....-(J=` ` ` ```....~~~: ....````` ` JdM[...`...`........`_3.. ..?!..(-7! ` ` ``````....~~~:: r...`````` ` ``(CJMb..............`......__..-(?^ ` ` `````....~~::; J/...````` ` `,3>+MN-.`..`...`..........._(J=`` ` ` ```````....~~~::; _j,..`.```` ``.5>>?dNb...`......`......_-?'` ` `````....~~~::;; ~~j,..```````.D??>>>MM/....`........(-=` ` ` ` ```````...~~~:::;> ~~~j,...````.E??>??>?MN-.........(J= ` ` ` ``````....~~~~::;?? :~~~?,...``.@>?>?>>??dMN_....-(?^ ` ` ` ` ````````...~~~~:;;>?? ::~~~?/....K??????>>?>dMN-_(7! ` ` ` ````````....~~~:::>>??l ;:::~~/e.(K==?????????<dM"! ` ` ` ` `` ``````...~~~~:::;>??=l */ #include "bits/stdc++.h" using namespace std; #define ok1 printf("ok1\n"); #define ok2 printf("ok2\n"); #define M 1000000000000000000LL #define rep(i, n) for (int i = 0; i < n; ++i) #define REP(i, s, n) for (int i = (s); i < (n); ++i) #define repr(i, n) for (int i = n - 1; i >= 0; --i) #define REPR(i, s, n) for (int i = (s); i >= (n); --(i)) #define all(a) (a).begin(), (a).end() #define reall(a) (a).rbegin(), (a).rend() #define pb push_back #define pf push_front #define MIN(a, b) a = min((a), (b)) #define MAX(a, b) a = max((a), (b)) #define SIZE(v) (int)v.size() #define DOUBLE fixed << setprecision(15) #define fi first #define se second const double pi = acos(-1.0); typedef vector<int> vi; typedef vector<string> vs; typedef long long ll; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<char> vc; typedef vector<double> vd; typedef vector<bool> vb; typedef deque<ll> dll; typedef pair<ll, ll> P; typedef vector<P> vP; const ll mod = 1e9 + 7; // const ll mod = 998244353; ll dy[4] = {1, 0, -1, 0}; ll dx[4] = {0, 1, 0, -1}; struct edge { int to; int cost; }; bool out_check(ll a, ll b) { if (0 <= a && a < b) return true; else return false; } void Pvll(vll v) { cout << "------------------------------------------------\n"; rep(i, v.size()) cout << v[i] << " "; cout << endl; cout << "------------------------------------------------\n"; } void Pvvll(vvll v) { cout << "------------------------------------------------\n"; rep(i, v.size()) { rep(j, v[i].size()) { cout << v[i][j] << " "; } cout << endl; } cout << "------------------------------------------------\n"; } void Ps(string s) { cout << "------------------------------------------------\n"; rep(i, s.size()) cout << s[i] << " "; cout << endl; cout << "------------------------------------------------\n"; } void Pvs(vs s) { cout << "------------------------------------------------\n"; rep(i, s.size()) { rep(j, s[i].size()) { cout << s[i][j] << " "; } cout << endl; } cout << "------------------------------------------------\n"; } void Yes(bool x) { if (x) cout << "Yes\n"; else cout << "No\n"; } void YES(bool x) { if (x) cout << "YES\n"; else cout << "NO\n"; } void yes(bool x) { if (x) cout << "yes\n"; else cout << "no\n"; } void Yay(bool x) { if (x) cout << "Yay!\n"; else cout << ":(\n"; } ll n, m, d, r, l, k, h, ans = 0, ret = M; bool flag = false, flag2 = false, flag3 = false; string s, t, u; void INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << DOUBLE; } int main() { INIT(); ll a, b, c; cin >> n; vll v(n + 2, 0); rep(i, n + 1) v[i + 1] = i; v[1] = n; rep(_, n) { ll i = _ + 1; ans += v[i] % i; } cout << ans << endl; return 0; }
/* vrrtll==???>;::::~~~~~~......`.`````````````````````...-?777!_.._?7u,~~~::::;>>??=lllttrrzu rtll==??>>;::::~~~~......`.`````````````````` ..J77!`````````...`..._T,~~~~:::;;>??==lttrrv tll=??>>;:::~~~~......``````````````````..J7^ ` `` ` `` .,```````...._4,.~~~::;;>>?===lttr l=???>;;::~~~......`````HTSu,.`` `..J7! ` `` .J"~N```````````..?&.~~~~::;;>>?==llt =??>;;::~~~~.....``````.@????7SJ.?= ` ` `` .J=....J; ``````````..h..~~~~::;;>??=ll ?>>;::~~~~.....````````.D?>>?>?>?8+.`` `.J"_......_b` ````````.S_.~~~~::;;>??=l >;;::~~~~....``````````.C>??>>>?>>>?8J.` ```..Y~..........J; ` ` ``````` G...~~~~::;>??= ;;::~~~....```````` `..W1>>?>>>>?>>>>>?S,.`.7^.............-N`` ` ``````` 6...~~~~::;>?? ;:~~~~....``````` ..7` d??>>?>?>>?>>?>>1udMgggNNNNggJ.......([ `````.L...~~~~::;>? :~~~.....`````` .7` `K>?>?>>>>>>+ugNMMMB""7<!~~<?7TWMNNa,..w.` ` ` ` `````,)....~~:::;> ~~~....``````.J^ ` #>>?>>>?jgMMM9=_................-?TMMa,b` ` ` ` ````(,...~~~~:;; ~~~....``` .7`` ` @?>>?1uMM#=.........................(TMNa...... ` ``````4....~~~::; ~~~...`` .=`` ` ` .b>>jgNH".................`.............?HNmx??17TYY9SA+(..L....~~~:: ~....` ,^`` ` ` .b+dN#^............6..-(,-...`............(HMm+>>>>>?>>????zOM_.~~~:: .... .=``` `` ` ...JNMM^..........`..._n.(MMN,....`..`.........?MNe<>>?>??>????d^...~~~: ~...v```` ` ..-Z""!_..(M@_........`........?7MMMMp.................-TNN+?>>>????1d4-..-(Jk9 ..(^`...zY"!_........(MD..............`......JMMMMp....`..`..`......./MNx>??>>?1d!.h7=~(??= (v_`,R_.............(NF..(/..(&,...`..........?MMMM;..................(MMs>>?1&T"!``....(1= t..`` 4,...........(N@....?,(NMNR_.....`..`....(WMM$..`....`..`..`....._HMe7=```````....~_1 ...````.4,........-dM:.....(7MMMNR-.....................`............(.?^ `` ``````....~~~ ...``````,4,....`.(NF........(MMMMb..`....--................`....(.7! ` ````....~~: ..````` ` `.5J_...JMt.........?NMMM[...`.HH5._(J7[...`...`...--?^` ` `````....~~~: ...````` ` ` 7a,.dM{....`...../MMMN......(J=` .>......._(/= ` ` `````...~~~: ....```` `` (4MN{..........._7"^...(/^ `.C....-(J=` ` ` ```....~~~: ....````` ` JdM[...`...`........`_3.. ..?!..(-7! ` ` ``````....~~~:: r...`````` ` ``(CJMb..............`......__..-(?^ ` ` `````....~~::; J/...````` ` `,3>+MN-.`..`...`..........._(J=`` ` ` ```````....~~~::; _j,..`.```` ``.5>>?dNb...`......`......_-?'` ` `````....~~~::;; ~~j,..```````.D??>>>MM/....`........(-=` ` ` ` ```````...~~~:::;> ~~~j,...````.E??>??>?MN-.........(J= ` ` ` ``````....~~~~::;?? :~~~?,...``.@>?>?>>??dMN_....-(?^ ` ` ` ` ````````...~~~~:;;>?? ::~~~?/....K??????>>?>dMN-_(7! ` ` ` ````````....~~~:::>>??l ;:::~~/e.(K==?????????<dM"! ` ` ` ` `` ``````...~~~~:::;>??=l */ #include "bits/stdc++.h" using namespace std; #define ok1 printf("ok1\n"); #define ok2 printf("ok2\n"); #define M 1000000000000000000LL #define rep(i, n) for (int i = 0; i < n; ++i) #define REP(i, s, n) for (int i = (s); i < (n); ++i) #define repr(i, n) for (int i = n - 1; i >= 0; --i) #define REPR(i, s, n) for (int i = (s); i >= (n); --(i)) #define all(a) (a).begin(), (a).end() #define reall(a) (a).rbegin(), (a).rend() #define pb push_back #define pf push_front #define MIN(a, b) a = min((a), (b)) #define MAX(a, b) a = max((a), (b)) #define SIZE(v) (int)v.size() #define DOUBLE fixed << setprecision(15) #define fi first #define se second const double pi = acos(-1.0); typedef vector<int> vi; typedef vector<string> vs; typedef long long ll; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<char> vc; typedef vector<double> vd; typedef vector<bool> vb; typedef deque<ll> dll; typedef pair<ll, ll> P; typedef vector<P> vP; const ll mod = 1e9 + 7; // const ll mod = 998244353; ll dy[4] = {1, 0, -1, 0}; ll dx[4] = {0, 1, 0, -1}; struct edge { int to; int cost; }; bool out_check(ll a, ll b) { if (0 <= a && a < b) return true; else return false; } void Pvll(vll v) { cout << "------------------------------------------------\n"; rep(i, v.size()) cout << v[i] << " "; cout << endl; cout << "------------------------------------------------\n"; } void Pvvll(vvll v) { cout << "------------------------------------------------\n"; rep(i, v.size()) { rep(j, v[i].size()) { cout << v[i][j] << " "; } cout << endl; } cout << "------------------------------------------------\n"; } void Ps(string s) { cout << "------------------------------------------------\n"; rep(i, s.size()) cout << s[i] << " "; cout << endl; cout << "------------------------------------------------\n"; } void Pvs(vs s) { cout << "------------------------------------------------\n"; rep(i, s.size()) { rep(j, s[i].size()) { cout << s[i][j] << " "; } cout << endl; } cout << "------------------------------------------------\n"; } void Yes(bool x) { if (x) cout << "Yes\n"; else cout << "No\n"; } void YES(bool x) { if (x) cout << "YES\n"; else cout << "NO\n"; } void yes(bool x) { if (x) cout << "yes\n"; else cout << "no\n"; } void Yay(bool x) { if (x) cout << "Yay!\n"; else cout << ":(\n"; } ll n, m, d, r, l, k, h, ans = 0, ret = M; bool flag = false, flag2 = false, flag3 = false; string s, t, u; void INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << DOUBLE; } int main() { INIT(); ll a, b, c; cin >> n; ans = n * (n + 1) / 2 - n; cout << ans << endl; return 0; }
replace
180
187
180
181
0
p02924
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; const ll INF = 1LL << 60; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int main(void) { ll i, j, k, ans = 0, cnt = 0, N, tmp, at, M = 0; scanf("%lld", &N); for (i = 1; i <= N - 1; i++) { j = i + 1; M += i % j; } printf("%lld", M); return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; const ll INF = 1LL << 60; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int main(void) { ll i, j, k, ans = 0, cnt = 0, N, tmp, at, M = 0; scanf("%lld", &N); for (i = 1; i <= N - 1; i++) { M += i; } printf("%lld", M); return 0; }
replace
25
27
25
26
TLE
p02924
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { long long n; cin >> n; long long ans = 0; for (int i = 1; i <= n; i++) { if (i == n) ans += i % 1; else ans += i % (i + 1); } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { long long n; cin >> n; long long ans = n * (n - 1) / 2; cout << ans << endl; return 0; }
replace
10
17
10
11
TLE
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define rrng(a) a.rbegin(), a.rend() #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define limit(x, l, r) max(l, min(x, r)) #define lims(x, l, r) (x = max(l, min(x, r))) #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)), x.end()) #define snuke srand((unsigned)clock() + (unsigned)time(NULL)); #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define bn(x) ((1 << x) - 1) #define dup(x, y) (((x) + (y)-1) / (y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; int main() { ll n; cin >> n; ll a, b; ll count = 0; for (int i = 1; i < n; i++) { a = i % (i + 1); count = count + a; } cout << count << endl; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define rrng(a) a.rbegin(), a.rend() #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define limit(x, l, r) max(l, min(x, r)) #define lims(x, l, r) (x = max(l, min(x, r))) #define isin(x, l, r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)), x.end()) #define snuke srand((unsigned)clock() + (unsigned)time(NULL)); #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define bn(x) ((1 << x) - 1) #define dup(x, y) (((x) + (y)-1) / (y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<T> vt; int main() { ll n; cin >> n; ll a = n * (n - 1) / 2; cout << a << endl; }
replace
42
49
42
45
TLE
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <math.h> #include <stdlib.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) inline constexpr ll gcd(ll a, ll b) { if (!a || !b) return 0; while (b) { ll c = b; b = a % b; a = c; } return a; } inline constexpr ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } int main() { long long a; cin >> a; long long sum = 0; rep(i, a - 1) { sum += (i + 1) % (i + 2); } cout << sum << endl; }
#include <bits/stdc++.h> #include <math.h> #include <stdlib.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) inline constexpr ll gcd(ll a, ll b) { if (!a || !b) return 0; while (b) { ll c = b; b = a % b; a = c; } return a; } inline constexpr ll lcm(ll a, ll b) { if (!a || !b) return 0; return a * b / gcd(a, b); } int main() { long long a; cin >> a; cout << a * (a - 1) / 2 << endl; }
replace
24
27
24
25
TLE
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(a) (a).begin(), (a).end() #define MOD 1000000007 int main() { int N; cin >> N; long long int sum = 0; for (int i = N; i > 1; i--) { sum += (i - 1 % i); // if(i == 1) sum += N % i; } if (N == 1) cout << 0; else cout << sum; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(a) (a).begin(), (a).end() #define MOD 1000000007 int main() { int N; cin >> N; long long int sum = 0; for (int i = N; i > 1; i--) sum += i - 1; if (N == 1) cout << 0; else cout << sum; }
replace
10
14
10
12
TLE
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define lli long long int #define MODV 1000000007 #define INF INT_MAX // 2147483647 #define INFLL LLONG_MAX // 9223372036854775807 #define rep(i, n) for (lli i = 0, i##_len = (lli)(n); i < i##_len; i++) #define repf(i, n) for (lli i = 1, i##_len = (lli)(n + 1); i < i##_len; i++) #define per(i, n) for (lli i = ((lli)(n)) - 1; i >= 0; i--) #define perf(i, n) for (lli i = ((lli)(n)); i > 0; i--) #define all(v) v.begin(), v.end() #define endl "\n" #define vi vector<lli> #define vvi vector<vector<lli>> #define Yes() cout << "Yes" << endl #define YES() cout << "YES" << endl #define No() cout << "No" << endl #define NO() cout << "NO" << endl 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; } using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); lli n; cin >> n; lli sum = 0; repf(i, n) if (i != n) sum += i % (i + 1); cout << sum << endl; }
#include <bits/stdc++.h> #define lli long long int #define MODV 1000000007 #define INF INT_MAX // 2147483647 #define INFLL LLONG_MAX // 9223372036854775807 #define rep(i, n) for (lli i = 0, i##_len = (lli)(n); i < i##_len; i++) #define repf(i, n) for (lli i = 1, i##_len = (lli)(n + 1); i < i##_len; i++) #define per(i, n) for (lli i = ((lli)(n)) - 1; i >= 0; i--) #define perf(i, n) for (lli i = ((lli)(n)); i > 0; i--) #define all(v) v.begin(), v.end() #define endl "\n" #define vi vector<lli> #define vvi vector<vector<lli>> #define Yes() cout << "Yes" << endl #define YES() cout << "YES" << endl #define No() cout << "No" << endl #define NO() cout << "NO" << endl 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; } using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); lli n; cin >> n; cout << (n * (n - 1) / 2) << endl; }
replace
39
42
39
40
TLE
p02924
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long int main() { ll sum = 0, ans = -1; ll n; cin >> n; for (ll i = 1; i <= n; i++) { sum += (i % n); } std::cout << sum << std::endl; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long int main() { ll sum = 0, ans = -1; ll n; cin >> n; n--; sum = (n * (n + 1)) / 2; std::cout << sum << std::endl; return 0; }
replace
10
13
10
12
TLE
p02924
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <vector> // #include <stdio.h> // #include <stack> // #include <queue> // #include <cstdio> #include <cmath> #include <fstream> #include <iterator> #include <map> // #include <list> #include <iomanip> // #include <stdlib.h> // #include <cctype> using namespace std; const int MAX_N = 1000000; #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define prec std::cout << std::fixed << std::setprecision(15); #define endl "\n" #define MOD 1000000007 #define Int int64_t #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < int(n); i++) #define ssort(z) sort(z.begin(), z.end()) #define rsort(z) sort(z.rbegin(), z.rend()) #define eerase(z) z.erase(unique(z.begin(), z.end()), z.end()) #define ccnt(z, w) count(z.begin(), z.end(), w) template <typename T> void printV(const std::vector<T> &v, const char *const separator = " ") { if (!v.empty()) { std::copy(v.begin(), --v.end(), std::ostream_iterator<T>(std::cout, separator)); std::cout << v.back() << "\n"; } } Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; } Int lcm(Int a, Int b) { return a / gcd(a, b) * b; // a*bは64bit integer overflow } int Max(int a, int b, int c) { int temp = max(a, b); return max(temp, c); } int Min(int a, int b, int c) { int temp = min(a, b); return min(temp, c); } bool integer(double num) { return floor(num) == num; } Int fact(int num) { if (num == 0) return 1; else return num * fact(num - 1); } void y() { cout << "Yes" << endl; exit(0); } void n() { cout << "No" << endl; exit(0); } void Y() { cout << "YES" << endl; exit(0); } void N() { cout << "NO" << endl; exit(0); } Int yakusu(int n) { int cnt = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { cnt++; if (i * i != n) cnt++; } } return cnt; } /* kokomade */ int cnt = 0; bool is_prime[50000 + 1]; vector<int> P; void Eratosthenes(const int N) { for (int i = 0; i <= N; i++) { is_prime[i] = true; // 初期化 } for (int i = 2; i <= N; i++) { if (is_prime[i]) { for (int j = 2 * i; j <= N; j += i) { is_prime[j] = false; } P.emplace_back(i); } } } int main() { IOS; // int n,x,t;cin>>n>>x>>t; // int a,b,c;cin>>a>>b>>c; // string str;cin>>str; // vector<int> l(n); // vector<string> s(n); Int n; cin >> n; Int ans; for (int i = 0; i < n; ++i) { Int num = i + 1; if (i < n - 1) { ans += num % (num + 1); } } cout << ans; cout << endl; return 0; } /* テレビも無ェ ラジオも無ェ 自動車もそれほど走って無ェ ピアノも無ェ バーも無ェ 巡査 毎日ぐーるぐる 朝起ぎで 牛連れで 二時間ちょっとの散歩道 電話も無ェ 瓦斯も無ェ バスは一日一度来る 俺らこんな村いやだ 俺らこんな村いやだ 東京へ出るだ 東京へ出だなら 銭コァ貯めで 東京でベコ(牛)飼うだ */
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <vector> // #include <stdio.h> // #include <stack> // #include <queue> // #include <cstdio> #include <cmath> #include <fstream> #include <iterator> #include <map> // #include <list> #include <iomanip> // #include <stdlib.h> // #include <cctype> using namespace std; const int MAX_N = 1000000; #define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define prec std::cout << std::fixed << std::setprecision(15); #define endl "\n" #define MOD 1000000007 #define Int int64_t #define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < int(n); i++) #define ssort(z) sort(z.begin(), z.end()) #define rsort(z) sort(z.rbegin(), z.rend()) #define eerase(z) z.erase(unique(z.begin(), z.end()), z.end()) #define ccnt(z, w) count(z.begin(), z.end(), w) template <typename T> void printV(const std::vector<T> &v, const char *const separator = " ") { if (!v.empty()) { std::copy(v.begin(), --v.end(), std::ostream_iterator<T>(std::cout, separator)); std::cout << v.back() << "\n"; } } Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; } Int lcm(Int a, Int b) { return a / gcd(a, b) * b; // a*bは64bit integer overflow } int Max(int a, int b, int c) { int temp = max(a, b); return max(temp, c); } int Min(int a, int b, int c) { int temp = min(a, b); return min(temp, c); } bool integer(double num) { return floor(num) == num; } Int fact(int num) { if (num == 0) return 1; else return num * fact(num - 1); } void y() { cout << "Yes" << endl; exit(0); } void n() { cout << "No" << endl; exit(0); } void Y() { cout << "YES" << endl; exit(0); } void N() { cout << "NO" << endl; exit(0); } Int yakusu(int n) { int cnt = 0; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { cnt++; if (i * i != n) cnt++; } } return cnt; } /* kokomade */ int cnt = 0; bool is_prime[50000 + 1]; vector<int> P; void Eratosthenes(const int N) { for (int i = 0; i <= N; i++) { is_prime[i] = true; // 初期化 } for (int i = 2; i <= N; i++) { if (is_prime[i]) { for (int j = 2 * i; j <= N; j += i) { is_prime[j] = false; } P.emplace_back(i); } } } int main() { IOS; // int n,x,t;cin>>n>>x>>t; // int a,b,c;cin>>a>>b>>c; // string str;cin>>str; // vector<int> l(n); // vector<string> s(n); Int n; cin >> n; Int ans = n * (n - 1) / 2; cout << ans; cout << endl; return 0; } /* テレビも無ェ ラジオも無ェ 自動車もそれほど走って無ェ ピアノも無ェ バーも無ェ 巡査 毎日ぐーるぐる 朝起ぎで 牛連れで 二時間ちょっとの散歩道 電話も無ェ 瓦斯も無ェ バスは一日一度来る 俺らこんな村いやだ 俺らこんな村いやだ 東京へ出るだ 東京へ出だなら 銭コァ貯めで 東京でベコ(牛)飼うだ */
replace
127
134
127
129
TLE
p02924
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N; cin >> N; vector<ll> P(N); vector<ll> M(N); if (N == 1) { cout << 0 << endl; return 0; } ll ans = N * (N - 1) / 2; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N; cin >> N; if (N == 1) { cout << 0 << endl; return 0; } ll ans = N * (N - 1) / 2; cout << ans << endl; return 0; }
delete
8
11
8
8
0
p02924
Python
Time Limit Exceeded
# -*- coding: utf-8 -*- n = int(input()) tmp = [i for i in range(1, n + 1)] print(sum(tmp) - n)
# -*- coding: utf-8 -*- n = int(input()) su = n * (n + 1) // 2 print(su - n)
replace
3
5
3
5
TLE
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <vector> #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vii vector<int> #define vll vector<ll> #define lb lower_bound #define pb push_back #define mp make_pair #define rep(i, n) for (ll i = 0; i < n; i++) #define rep2(i, a, b) for (ll i = a; i < b; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define all(x) x.begin(), x.end() // #define int ll using namespace std; const int INF(1 << 30); const ll LLINF(1LL << 55LL); const int MOD = 1000000007; const int MAX = 510000; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); chrono::system_clock::time_point start, end; start = chrono::system_clock::now(); ll n, a[1010][1010], ans = -1; bool b[1010] = {}; vll v[1010]; cin >> n; rep(i, n) rep(j, n - 1) { cin >> a[i][j]; v[i + 1].pb(a[i][j]); } rep(i, n * (n - 1) / 2 + 1) { end = chrono::system_clock::now(); double time = (double)(chrono::duration_cast<chrono::microseconds>(end - start) .count() / 1000.0); if (time >= 40000) { cout << -1 << endl; return 0; } bool flag = false; rep2(j, 1, n + 1) b[j] = 0; rep2(j, 1, n + 1) { if (!v[j].empty()) { flag = true; break; } } if (!flag) { ans = i; break; } rep2(j, 1, n + 1) { if (b[j] || v[j].empty()) continue; ll u = v[j][0]; if (b[u]) continue; if (v[u].empty()) { cout << -1 << endl; return 0; } if (v[u][0] != j) continue; b[j] = 1; b[u] = 1; v[j].erase(v[j].begin()); v[u].erase(v[u].begin()); } rep2(j, 1, n + 1) { if (b[j]) flag = false; } if (flag) { cout << -1 << endl; return 0; } } cout << ans << endl; return 0; }
#include <algorithm> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <vector> #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vii vector<int> #define vll vector<ll> #define lb lower_bound #define pb push_back #define mp make_pair #define rep(i, n) for (ll i = 0; i < n; i++) #define rep2(i, a, b) for (ll i = a; i < b; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define all(x) x.begin(), x.end() // #define int ll using namespace std; const int INF(1 << 30); const ll LLINF(1LL << 55LL); const int MOD = 1000000007; const int MAX = 510000; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); chrono::system_clock::time_point start, end; start = chrono::system_clock::now(); ll n, a[1010][1010], ans = -1; bool b[1010] = {}; vll v[1010]; cin >> n; rep(i, n) rep(j, n - 1) { cin >> a[i][j]; v[i + 1].pb(a[i][j]); } rep(i, n * (n - 1) / 2 + 1) { end = chrono::system_clock::now(); double time = (double)(chrono::duration_cast<chrono::microseconds>(end - start) .count() / 1000.0); if (time >= 1950) { cout << (n - 1) * n / 2 << endl; return 0; } bool flag = false; rep2(j, 1, n + 1) b[j] = 0; rep2(j, 1, n + 1) { if (!v[j].empty()) { flag = true; break; } } if (!flag) { ans = i; break; } rep2(j, 1, n + 1) { if (b[j] || v[j].empty()) continue; ll u = v[j][0]; if (b[u]) continue; if (v[u].empty()) { cout << -1 << endl; return 0; } if (v[u][0] != j) continue; b[j] = 1; b[u] = 1; v[j].erase(v[j].begin()); v[u].erase(v[u].begin()); } rep2(j, 1, n + 1) { if (b[j]) flag = false; } if (flag) { cout << -1 << endl; return 0; } } cout << ans << endl; return 0; }
replace
58
60
58
60
TLE
p02925
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define sbits(a) __builtin_popcount(a) #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define gcd __gcd #define lcm(a, b) ((a * b) / gcd(a, b)) #define pq priority_queue #define um unordered_map #define us unordered_set #define lb lower_bound #define ub upper_bound #define F first #define B begin() #define E end() #define ln length() #define sz size() #define S second #define em empty() #define nani(x) cerr << #x << " is " << x << endl; #define ch(x) (x - 'a' + 1) #define mp make_pair #define pb push_back #define loop(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define scan(x, n) loop(i, 0, n) cin >> x[i]; #define adde(g, u, v) g[u].pb(v), g[v].pb(u); #define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w}); #define print(x, n) loop(i, 0, n) cout << x[i] << " "; #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define inf 10000000000 #define mod 1000000007 using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long> vl; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<long, long> pll; typedef vector<ii> vii; typedef vector<pll> vll; typedef um<int, int> umii; typedef um<char, int> umci; typedef map<char, int> mci; typedef map<int, int> mii; const int N = 1e5 + 1; vi G[N]; int in[N]; int unique(int a, int b) { return min(a, b) * 1000 + max(a, b); } int main() { fast; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n; cin >> n; for (int i = 1; i <= n; i++) { int prev; cin >> prev; for (int j = 2; j < n; j++) { int cur; cin >> cur; int a = unique(i, cur); int b = unique(i, prev); in[a]++; G[b].pb(a); prev = cur; } } vi v; for (int i = 0; i < N; i++) if (!in[i]) v.pb(i); int total = 0; int day = 0; while (v.sz) { total += v.sz; ++day; vi z; for (auto &i : v) { for (auto &j : G[i]) { --in[j]; if (!in[j]) z.pb(j); } } v = z; } if (total != N) cout << -1; else cout << day; return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define sbits(a) __builtin_popcount(a) #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define gcd __gcd #define lcm(a, b) ((a * b) / gcd(a, b)) #define pq priority_queue #define um unordered_map #define us unordered_set #define lb lower_bound #define ub upper_bound #define F first #define B begin() #define E end() #define ln length() #define sz size() #define S second #define em empty() #define nani(x) cerr << #x << " is " << x << endl; #define ch(x) (x - 'a' + 1) #define mp make_pair #define pb push_back #define loop(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define scan(x, n) loop(i, 0, n) cin >> x[i]; #define adde(g, u, v) g[u].pb(v), g[v].pb(u); #define addew(g, u, v, w) g[u].pb({v, w}), g[v].pb({u, w}); #define print(x, n) loop(i, 0, n) cout << x[i] << " "; #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define inf 10000000000 #define mod 1000000007 using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long> vl; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<long, long> pll; typedef vector<ii> vii; typedef vector<pll> vll; typedef um<int, int> umii; typedef um<char, int> umci; typedef map<char, int> mci; typedef map<int, int> mii; const int N = 1e6 + 1; vi G[N]; int in[N]; int unique(int a, int b) { return min(a, b) * 1000 + max(a, b); } int main() { fast; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n; cin >> n; for (int i = 1; i <= n; i++) { int prev; cin >> prev; for (int j = 2; j < n; j++) { int cur; cin >> cur; int a = unique(i, cur); int b = unique(i, prev); in[a]++; G[b].pb(a); prev = cur; } } vi v; for (int i = 0; i < N; i++) if (!in[i]) v.pb(i); int total = 0; int day = 0; while (v.sz) { total += v.sz; ++day; vi z; for (auto &i : v) { for (auto &j : G[i]) { --in[j]; if (!in[j]) z.pb(j); } } v = z; } if (total != N) cout << -1; else cout << day; return 0; }
replace
64
65
64
65
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _MACRO(_1, _2, _3, NAME, ...) NAME #define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define _rep(i, n) _repl(i, 0, n) #define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__) #define pb push_back #define all(x) begin(x), end(x) #define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x)) #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string) { cerr << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find(','); cerr << s.substr(0, l) << " = " << h << ", "; _dbg(s.substr(l + 1), t...); } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << "(" << p.first << "," << p.second << ")"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } #else #define dbg(...) \ {} #endif class SCC { private: vector<vector<int>> &G; vector<vector<int>> &rG; vector<bool> used; vector<int> vs; int n; void dfs(int v) { used[v] = true; for (auto to : G[v]) if (!used[to]) dfs(to); vs.pb(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; groups.back().pb(v); for (auto to : rG[v]) if (!used[to]) rdfs(to, k); } public: vector<int> cmp; // 頂点iが属するSCCのID vector<vector<int>> groups; // SCCのトポロジカル順i番目に属する頂点番号群 int gc = 0; // group count SCC(vector<vector<int>> &g, vector<vector<int>> &r) : G(g), rG(r) { n = G.size(); used = vector<bool>(n, false); cmp.resize(n); rep(i, n) if (!used[i]) dfs(i); fill(all(used), false); for (int i = vs.size() - 1; i >= 0; i--) if (!used[vs[i]]) { groups.pb(vector<int>()); rdfs(vs[i], gc++); } } inline bool same(int i, int j) { return cmp[i] == cmp[j]; } inline int operator[](int i) { return cmp[i]; } }; int main() { int n; cin >> n; map<pair<int, int>, int> num; rep(i, n) rep(j, i) num[{j, i}] = num.size(); vector<vector<int>> to(num.size()); vector<vector<int>> from(num.size()); rep(i, n) { vector<int> tmp; rep(j, n - 1) { int k; cin >> k; k--; tmp.pb(num[{min(i, k), max(i, k)}]); } rep(j, n - 2) { to[tmp[j]].pb(tmp[j + 1]); from[tmp[j + 1]].pb(tmp[j]); } } SCC scc(to, from); for (auto &v : scc.groups) if (v.size() > 1) { cout << -1 << endl; return 0; } vector<int> nxt; vector<int> d(num.size()); rep(i, num.size()) { d[i] = from[i].size(); if (d[i] == 0) nxt.pb(i); } int ans = 0; int visited = n * (n - 1) / 2; while (visited > 0) { ans++; vector<int> tmp; assert(nxt.size() > 0); for (auto v : nxt) { visited--; for (auto i : to[v]) { d[i]--; if (d[i] == 0) tmp.pb(i); } } swap(tmp, nxt); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define _MACRO(_1, _2, _3, NAME, ...) NAME #define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define _rep(i, n) _repl(i, 0, n) #define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__) #define pb push_back #define all(x) begin(x), end(x) #define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x)) #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string) { cerr << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find(','); cerr << s.substr(0, l) << " = " << h << ", "; _dbg(s.substr(l + 1), t...); } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << "(" << p.first << "," << p.second << ")"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } #else #define dbg(...) \ {} #endif class SCC { private: vector<vector<int>> &G; vector<vector<int>> &rG; vector<bool> used; vector<int> vs; int n; void dfs(int v) { used[v] = true; for (auto to : G[v]) if (!used[to]) dfs(to); vs.pb(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; groups.back().pb(v); for (auto to : rG[v]) if (!used[to]) rdfs(to, k); } public: vector<int> cmp; // 頂点iが属するSCCのID vector<vector<int>> groups; // SCCのトポロジカル順i番目に属する頂点番号群 int gc = 0; // group count SCC(vector<vector<int>> &g, vector<vector<int>> &r) : G(g), rG(r) { n = G.size(); used = vector<bool>(n, false); cmp.resize(n); rep(i, n) if (!used[i]) dfs(i); fill(all(used), false); for (int i = vs.size() - 1; i >= 0; i--) if (!used[vs[i]]) { groups.pb(vector<int>()); rdfs(vs[i], gc++); } } inline bool same(int i, int j) { return cmp[i] == cmp[j]; } inline int operator[](int i) { return cmp[i]; } }; int main() { int n; cin >> n; map<pair<int, int>, int> num; rep(i, n) rep(j, i) { int x = num.size(); num[{j, i}] = x; } vector<vector<int>> to(num.size()); vector<vector<int>> from(num.size()); rep(i, n) { vector<int> tmp; rep(j, n - 1) { int k; cin >> k; k--; tmp.pb(num[{min(i, k), max(i, k)}]); } rep(j, n - 2) { to[tmp[j]].pb(tmp[j + 1]); from[tmp[j + 1]].pb(tmp[j]); } } SCC scc(to, from); for (auto &v : scc.groups) if (v.size() > 1) { cout << -1 << endl; return 0; } vector<int> nxt; vector<int> d(num.size()); rep(i, num.size()) { d[i] = from[i].size(); if (d[i] == 0) nxt.pb(i); } int ans = 0; int visited = n * (n - 1) / 2; while (visited > 0) { ans++; vector<int> tmp; assert(nxt.size() > 0); for (auto v : nxt) { visited--; for (auto i : to[v]) { d[i]--; if (d[i] == 0) tmp.pb(i); } } swap(tmp, nxt); } cout << ans << endl; return 0; }
replace
82
83
82
86
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define ALL(a) (a).begin(), (a).end() #define rALL(a) (a).rbegin(), (a).rend() #define SZ(x) ((int)(x).size()) using ll = long long; using P = pair<ll, ll>; using llP = pair<ll, ll>; using DoP = pair<double, double>; const int di[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1}; const int INF = 1 << 29; const ll INF64 = 1ll << 62; const int mod = 1000000007; // const int mod = 998244353; template <class T> inline bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline void line_out(const vector<T> vec) { int n = SZ(vec); rep(i, n) { cout << vec[i]; // if(i < n-1) cout << " "; } cout << endl; return; } struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint &operator++() { if ((x += 1) >= mod) x -= mod; return *this; } mint &operator--() { if ((x += mod - 1) >= mod) x -= mod; return *this; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) rep(j, n - 1) { cin >> a[i][j]; --a[i][j]; } vector<int> now(n, 0); vector<bool> yesterday(n, true); for (int day = 0;; ++day) { vector<bool> f(n, false); bool update = false; rep(i, n) { if (!yesterday[i]) continue; int j = a[i][now[i]]; if (f[i] || f[j]) continue; if (a[j][now[j]] != i || now[i] == n - 1) { yesterday[i] = false; continue; } update = true; ++now[i]; ++now[j]; yesterday[i] = true; yesterday[j] = true; f[i] = true; f[j] = true; } if (!update) { rep(i, n) if (now[i] != n - 1) { cout << -1 << endl; return 0; } cout << day << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define ALL(a) (a).begin(), (a).end() #define rALL(a) (a).rbegin(), (a).rend() #define SZ(x) ((int)(x).size()) using ll = long long; using P = pair<ll, ll>; using llP = pair<ll, ll>; using DoP = pair<double, double>; const int di[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1}; const int INF = 1 << 29; const ll INF64 = 1ll << 62; const int mod = 1000000007; // const int mod = 998244353; template <class T> inline bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline void line_out(const vector<T> vec) { int n = SZ(vec); rep(i, n) { cout << vec[i]; // if(i < n-1) cout << " "; } cout << endl; return; } struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint &operator++() { if ((x += 1) >= mod) x -= mod; return *this; } mint &operator--() { if ((x += mod - 1) >= mod) x -= mod; return *this; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) rep(j, n - 1) { cin >> a[i][j]; --a[i][j]; } vector<int> now(n, 0); vector<bool> yesterday(n, true); for (int day = 0;; ++day) { vector<bool> f(n, false); bool update = false; rep(i, n) { if (!yesterday[i]) continue; int j = a[i][now[i]]; if (f[i] || f[j]) continue; if (now[i] == n - 1 || a[j][now[j]] != i) { yesterday[i] = false; continue; } update = true; ++now[i]; ++now[j]; yesterday[i] = true; yesterday[j] = true; f[i] = true; f[j] = true; } if (!update) { rep(i, n) if (now[i] != n - 1) { cout << -1 << endl; return 0; } cout << day << endl; return 0; } } }
replace
121
122
121
122
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> #ifdef TEST #include <mytest.h> #else #define open(...) 0 #define print(...) 42 #endif using namespace std; namespace io { const int SZ = (1 << 22) + 1; char buf[SZ], *ptr = NULL, *bnd = NULL; #define GC() \ ((ptr == bnd) ? (ptr = buf, bnd = buf + fread(buf, 1, SZ, stdin), \ (ptr == bnd) ? EOF : (*(ptr++))) \ : (*(ptr++))) #define STATE(c) \ { \ if (c == '-') \ sgn = -1; \ else if (c == EOF) \ return false; \ } inline bool skip(const char &c) { return c < '0' || c > '9'; } template <class V> inline bool Read(V &v) { register char c, sgn = 1; while (skip(c = GC())) STATE(c); for (v = c - '0'; !skip(c = GC()); v = v * 10 + c - '0') ; return (v *= sgn), true; } char oBuf[SZ], *oCur = oBuf, *oBnd = oBuf + SZ, oStk[21], top = 0; inline void flush() { if (oCur - oBuf) fwrite(oBuf, 1, oCur - oBuf, stdout), oCur = oBuf; } inline void pc(char c) { *(oCur++) = c; if (oCur == oBnd) flush(); } template <class V> inline void Print(V v) { if (!v) return pc('0'); if (v < 0) v = -v, pc('-'); while (v) oStk[top++] = v % 10, v /= 10; while (top) pc(oStk[--top] + '0'); } template <class V> inline void Println(const V &v) { Print(v), pc('\n'); } struct flusher { ~flusher() { flush(); } } __flusher__; } // namespace io using io::Println; using io::Read; const int N = 1024; int a[N][N]; bool vis[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; // cin >> n; Read(n); for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { // cin >> a[i][j]; Read(a[i][j]); } a[i][0] = 1; } int ans = 0, flg = 1, cnt = 0; while (flg) { flg = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (a[i][0] == n || vis[i]) continue; int t = a[i][a[i][0]]; if (i == a[t][a[t][0]] && !vis[t]) { flg = 1; vis[i] = vis[t] = 1, cnt++; a[t][0]++, a[i][0]++; } } if (flg) ans++; } if (cnt != n * (n - 1) / 2) ans = -1; cout << ans << endl; }
#include <bits/stdc++.h> #ifdef TEST #include <mytest.h> #else #define open(...) 0 #define print(...) 42 #endif using namespace std; namespace io { const int SZ = (1 << 22) + 1; char buf[SZ], *ptr = NULL, *bnd = NULL; #define GC() \ ((ptr == bnd) ? (ptr = buf, bnd = buf + fread(buf, 1, SZ, stdin), \ (ptr == bnd) ? EOF : (*(ptr++))) \ : (*(ptr++))) #define STATE(c) \ { \ if (c == '-') \ sgn = -1; \ else if (c == EOF) \ return false; \ } inline bool skip(const char &c) { return c < '0' || c > '9'; } template <class V> inline bool Read(V &v) { register char c, sgn = 1; while (skip(c = GC())) STATE(c); for (v = c - '0'; !skip(c = GC()); v = v * 10 + c - '0') ; return (v *= sgn), true; } char oBuf[SZ], *oCur = oBuf, *oBnd = oBuf + SZ, oStk[21], top = 0; inline void flush() { if (oCur - oBuf) fwrite(oBuf, 1, oCur - oBuf, stdout), oCur = oBuf; } inline void pc(char c) { *(oCur++) = c; if (oCur == oBnd) flush(); } template <class V> inline void Print(V v) { if (!v) return pc('0'); if (v < 0) v = -v, pc('-'); while (v) oStk[top++] = v % 10, v /= 10; while (top) pc(oStk[--top] + '0'); } template <class V> inline void Println(const V &v) { Print(v), pc('\n'); } struct flusher { ~flusher() { flush(); } } __flusher__; } // namespace io using io::Println; using io::Read; const int N = 1005; int a[N][N]; bool vis[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; // cin >> n; Read(n); for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { // cin >> a[i][j]; Read(a[i][j]); } a[i][0] = 1; } int ans = 0, flg = 1, cnt = 0; while (flg) { flg = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (a[i][0] == n || vis[i]) continue; int t = a[i][a[i][0]]; if (i == a[t][a[t][0]] && !vis[t]) { flg = 1; vis[i] = vis[t] = 1, cnt++; a[t][0]++, a[i][0]++; } } if (flg) ans++; } if (cnt != n * (n - 1) / 2) ans = -1; cout << ans << endl; }
replace
58
59
58
59
TLE
p02925
C++
Runtime Error
#include <algorithm> #include <iostream> #include <stdio.h> #include <vector> using namespace std; const int maxn = 1000 + 10; int n; int aa[maxn]; int b[maxn][maxn]; vector<int> a[maxn * maxn]; int d[maxn], deg[maxn] = {0}; int queue[maxn * maxn], front, rear; int kq; int id(int i, int j) { return b[i][j]; } int main() { // freopen("input.txt","r",stdin); scanf("%d", &n); int nn = 0; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { nn++; b[i][j] = b[j][i] = nn; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) scanf("%d", &aa[j]); for (int j = 1; j <= n - 2; j++) { int u = id(i, aa[j]); int v = id(i, aa[j + 1]); // cout<<"make("<<u<<"->"<<v<<")\n"; a[u].push_back(v); deg[v]++; } } // cout<<"NN= "<<nn<<endl; front = 1; rear = 0; for (int i = 1; i <= nn; i++) d[i] = -1; for (int i = 1; i <= nn; i++) if (deg[i] == 0) { rear++; queue[rear] = i; d[i] = 1; } while (front <= rear) { int u = queue[front]; front++; for (int i = 0; i < a[u].size(); i++) { int v = a[u][i]; if (deg[v] > 0) { deg[v]--; if (deg[v] == 0) { d[v] = d[u] + 1; rear++; queue[rear] = v; } } } } if (rear < nn) cout << -1; else { kq = 0; for (int i = 1; i <= nn; i++) kq = max(kq, d[i]); cout << kq; } }
#include <algorithm> #include <iostream> #include <stdio.h> #include <vector> using namespace std; const int maxn = 1000 + 10; int n; int aa[maxn]; int b[maxn][maxn]; vector<int> a[maxn * maxn]; int d[maxn * maxn], deg[maxn * maxn] = {0}; int queue[maxn * maxn], front, rear; int kq; int id(int i, int j) { return b[i][j]; } int main() { // freopen("input.txt","r",stdin); scanf("%d", &n); int nn = 0; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { nn++; b[i][j] = b[j][i] = nn; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) scanf("%d", &aa[j]); for (int j = 1; j <= n - 2; j++) { int u = id(i, aa[j]); int v = id(i, aa[j + 1]); // cout<<"make("<<u<<"->"<<v<<")\n"; a[u].push_back(v); deg[v]++; } } // cout<<"NN= "<<nn<<endl; front = 1; rear = 0; for (int i = 1; i <= nn; i++) d[i] = -1; for (int i = 1; i <= nn; i++) if (deg[i] == 0) { rear++; queue[rear] = i; d[i] = 1; } while (front <= rear) { int u = queue[front]; front++; for (int i = 0; i < a[u].size(); i++) { int v = a[u][i]; if (deg[v] > 0) { deg[v]--; if (deg[v] == 0) { d[v] = d[u] + 1; rear++; queue[rear] = v; } } } } if (rear < nn) cout << -1; else { kq = 0; for (int i = 1; i <= nn; i++) kq = max(kq, d[i]); cout << kq; } }
replace
11
12
11
12
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); for (int i = 0; i < n; i++) { for (int j = n - 2; j >= 0; j--) { int x; cin >> x; a[i][j] = x - 1; } } int TOT = n * (n - 1) / 2; set<int> now; for (int i = 0; i < n; i++) { now.insert(i); } int tot = TOT; for (int d = 1; d <= TOT; d++) { int cnt = 0; set<int> later; for (int i : now) { if (a[i].empty() || later.find(i) != later.end()) continue; int j = a[i].back(); if (later.find(j) != later.end() || a[j].empty() || a[j].back() != i) continue; later.insert(i); later.insert(j); a[i].pop_back(); a[j].pop_back(); cnt++; } if (cnt == 0) { cout << -1; return; } tot -= cnt; if (tot == 0) { cout << d; return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); for (int i = 0; i < n; i++) { for (int j = n - 2; j >= 0; j--) { int x; cin >> x; a[i][j] = x - 1; } } int TOT = n * (n - 1) / 2; set<int> now; for (int i = 0; i < n; i++) { now.insert(i); } int tot = TOT; for (int d = 1; d <= TOT; d++) { int cnt = 0; set<int> later; for (int i : now) { if (a[i].empty() || later.find(i) != later.end()) continue; int j = a[i].back(); if (later.find(j) != later.end() || a[j].empty() || a[j].back() != i) continue; later.insert(i); later.insert(j); a[i].pop_back(); a[j].pop_back(); cnt++; } if (cnt == 0) { cout << -1; return; } tot -= cnt; if (tot == 0) { cout << d; return; } now = later; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
insert
45
45
45
46
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lson rt << 1 #define rson rt << 1 | 1 #define scd(x) scanf("%d", &x) #define scdd(x, y) scanf("%d%d", &x, &y) const int maxn = 1000 + 10; typedef long long ll; typedef pair<int, int> pii; const int INF = 0x3f3f3f3f; map<pii, int> mp; int arr[maxn][maxn]; int in[maxn * maxn]; vector<int> G[maxn]; pii get_pii(int i, int j) { int mn = min(i, arr[i][j]), mx = max(i, arr[i][j]); return pii(mn, mx); } int topo(int n) { queue<int> q; for (int i = 1; i <= n; i++) { if (in[i] == 0) q.push(i); } int ans = 0; while (!q.empty()) { int u = q.front(); q.pop(); ans++; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; in[v]--; if (in[v] == 0) q.push(v); } } return ans; } int dp[maxn * maxn]; int dfs(int u) { if (~dp[u]) return dp[u]; int mx = 0; for (int i = 0; i < G[u].size(); i++) { mx = max(mx, dfs(G[u][i])); } return dp[u] = mx + 1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL int n; scd(n); int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) { scd(arr[i][j]); pii no = get_pii(i, j); if (mp[no] == 0) { mp[no] = ++cnt; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 2; j++) { pii one = get_pii(i, j), two = get_pii(i, j + 1); int on = mp[one], tw = mp[two]; G[on].push_back(tw); in[tw]++; } } int ans = topo(cnt); if (ans == cnt) { memset(dp, -1, sizeof(dp)); ans = 0; for (int i = 1; i <= cnt; i++) { ans = max(ans, dfs(i)); } cout << ans << endl; } else cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; #define lson rt << 1 #define rson rt << 1 | 1 #define scd(x) scanf("%d", &x) #define scdd(x, y) scanf("%d%d", &x, &y) const int maxn = 1000 + 10; typedef long long ll; typedef pair<int, int> pii; const int INF = 0x3f3f3f3f; map<pii, int> mp; int arr[maxn][maxn], in[maxn * maxn]; vector<int> G[maxn * maxn]; pii get_pii(int i, int j) { int mn = min(i, arr[i][j]), mx = max(i, arr[i][j]); return pii(mn, mx); } int topo(int n) { queue<int> q; for (int i = 1; i <= n; i++) { if (in[i] == 0) q.push(i); } int ans = 0; while (!q.empty()) { int u = q.front(); q.pop(); ans++; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; in[v]--; if (in[v] == 0) q.push(v); } } return ans; } int dp[maxn * maxn]; int dfs(int u) { if (~dp[u]) return dp[u]; int mx = 0; for (int i = 0; i < G[u].size(); i++) { mx = max(mx, dfs(G[u][i])); } return dp[u] = mx + 1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL int n; scd(n); int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) { scd(arr[i][j]); pii no = get_pii(i, j); if (mp[no] == 0) { mp[no] = ++cnt; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 2; j++) { pii one = get_pii(i, j), two = get_pii(i, j + 1); int on = mp[one], tw = mp[two]; G[on].push_back(tw); in[tw]++; } } int ans = topo(cnt); if (ans == cnt) { memset(dp, -1, sizeof(dp)); ans = 0; for (int i = 1; i <= cnt; i++) { ans = max(ans, dfs(i)); } cout << ans << endl; } else cout << -1 << endl; }
replace
11
14
11
13
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)a, i##_len = (b); i < i##_len; i++) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define all(box) box.begin(), box.end() using namespace std; using P = pair<int, int>; using ll = long long; const long long INF = LLONG_MAX / 3; 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; } struct UnionFind { std::vector<int> par, siz; void init(int n) { par.resize(n); for (int i = 0; i < n; i++) par[i] = i; siz.resize(n, 1); } UnionFind(int n) { init(n); } int root(int n) { if (par[n] == n) return n; else return par[n] = root(par[n]); } bool issame(int x, int y) { return (root(x) == root(y)); } bool marge(int a, int b) { if (issame(a, b)) return false; else { a = root(a), b = root(b); if (siz[a] < siz[b]) swap(a, b); siz[a] += siz[b]; par[a] = par[b]; return true; } } int size(int n) { return siz[root(n)]; } }; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long i, long long j) { return i * (j / gcd(i, j)); } const int MOD = 1e9 + 7; long long fac[510000], finv[510000], inv[510000]; long long MAX = 510000; bool COMinited = false; long long COM(int n, int k) { if (COMinited == false) { 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; } COMinited = true; } if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % 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; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int incnt[100010]; vector<int> e[100010]; int in[1010][1010]; int ind(int a, int b) { if (a > b) swap(a, b); return in[a][b]; } signed main() { int n; cin >> n; rep(i, n) { rep(j, i + 1, n) { if (i == 0 && j == 1) in[i][j] = 0; else if (j == i + 1) in[i][j] = in[i - 1][n - 1] + 1; else in[i][j] = in[i][j - 1] + 1; } } rep(i, n) { int a[1010]; rep(j, n - 1) { cin >> a[j]; a[j]--; } rep(j, n - 2) { e[ind(i, a[j])].push_back(ind(i, a[j + 1])); incnt[ind(i, a[j + 1])]++; } } stack<int> s; rep(i, ind(n - 2, n - 1) + 1) { if (incnt[i] == 0) { s.push(i); } } int ans = 0; vector<int> g; while (!s.empty()) { ans++; stack<int> t; while (!s.empty()) { int a = s.top(); s.pop(); t.push(a); } while (!t.empty()) { int a = t.top(); t.pop(); g.push_back(a); rep(i, e[a].size()) { incnt[e[a][i]]--; if (incnt[e[a][i]] == 0) { s.push(e[a][i]); } } } } if (g.size() == ind(n - 2, n - 1) + 1) cout << ans << endl; else cout << -1 << endl; }
#include <bits/stdc++.h> #define int long long #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)a, i##_len = (b); i < i##_len; i++) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define all(box) box.begin(), box.end() using namespace std; using P = pair<int, int>; using ll = long long; const long long INF = LLONG_MAX / 3; 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; } struct UnionFind { std::vector<int> par, siz; void init(int n) { par.resize(n); for (int i = 0; i < n; i++) par[i] = i; siz.resize(n, 1); } UnionFind(int n) { init(n); } int root(int n) { if (par[n] == n) return n; else return par[n] = root(par[n]); } bool issame(int x, int y) { return (root(x) == root(y)); } bool marge(int a, int b) { if (issame(a, b)) return false; else { a = root(a), b = root(b); if (siz[a] < siz[b]) swap(a, b); siz[a] += siz[b]; par[a] = par[b]; return true; } } int size(int n) { return siz[root(n)]; } }; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long i, long long j) { return i * (j / gcd(i, j)); } const int MOD = 1e9 + 7; long long fac[510000], finv[510000], inv[510000]; long long MAX = 510000; bool COMinited = false; long long COM(int n, int k) { if (COMinited == false) { 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; } COMinited = true; } if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % 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; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } int incnt[1000010]; vector<int> e[1000010]; int in[1010][1010]; int ind(int a, int b) { if (a > b) swap(a, b); return in[a][b]; } signed main() { int n; cin >> n; rep(i, n) { rep(j, i + 1, n) { if (i == 0 && j == 1) in[i][j] = 0; else if (j == i + 1) in[i][j] = in[i - 1][n - 1] + 1; else in[i][j] = in[i][j - 1] + 1; } } rep(i, n) { int a[1010]; rep(j, n - 1) { cin >> a[j]; a[j]--; } rep(j, n - 2) { e[ind(i, a[j])].push_back(ind(i, a[j + 1])); incnt[ind(i, a[j + 1])]++; } } stack<int> s; rep(i, ind(n - 2, n - 1) + 1) { if (incnt[i] == 0) { s.push(i); } } int ans = 0; vector<int> g; while (!s.empty()) { ans++; stack<int> t; while (!s.empty()) { int a = s.top(); s.pop(); t.push(a); } while (!t.empty()) { int a = t.top(); t.pop(); g.push_back(a); rep(i, e[a].size()) { incnt[e[a][i]]--; if (incnt[e[a][i]] == 0) { s.push(e[a][i]); } } } } if (g.size() == ind(n - 2, n - 1) + 1) cout << ans << endl; else cout << -1 << endl; }
replace
103
105
103
105
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define boost \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define rando \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define fi first #define se second #define debug(x) cerr << " - " << #x << ": " << x << endl; #define debugs(x, y) \ cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl; #define debugii(x) cerr << " - " << #x << ": " << x.fi << "," << x.se << endl; #define sep() cerr << "--------------------" << endl; #define all(x) (x).begin(), (x).end() #define sz(x) (ll) x.size() #define ll long long #define ii pair<int, int> #define v vector<int> #define vii vector<ii> #define vv vector<vector<int>> #define mp make_pair #define INF 1000000000 #define pb push_back #define EPS 1e-9 const int MOD = 1000000007; // 998244353 queue<int> tab[1002]; int vis[1002]; int idx = 1; int main() { boost; int n, a; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n - 1; ++j) { cin >> a; a--; tab[i].push(a); } int comp = (n * (n - 1LL)) / 2LL; // debug(comp); int ans = 0; while (comp) { bool f = 0; for (int i = 0; i < n; ++i) { if (vis[i] == idx || tab[i].empty()) continue; int adv = tab[i].front(); if (vis[adv] != idx && !tab[adv].empty() && tab[adv].front() == i) { tab[i].pop(); tab[adv].pop(); vis[adv] = vis[i] = idx; f = 1; comp--; } if (!comp) break; } if (!f) break; ans++, idx++; } if (!comp) cout << ans; else cout << -1; return 0; } // long long // array bounds // special cases // binary search
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define boost \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define rando \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define fi first #define se second #define debug(x) cerr << " - " << #x << ": " << x << endl; #define debugs(x, y) \ cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl; #define debugii(x) cerr << " - " << #x << ": " << x.fi << "," << x.se << endl; #define sep() cerr << "--------------------" << endl; #define all(x) (x).begin(), (x).end() #define sz(x) (ll) x.size() #define ll long long #define ii pair<int, int> #define v vector<int> #define vii vector<ii> #define vv vector<vector<int>> #define mp make_pair #define INF 1000000000 #define pb push_back #define EPS 1e-9 const int MOD = 1000000007; // 998244353 queue<int> tab[1002]; int vis[1002]; int idx = 1; int main() { boost; int n, a; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n - 1; ++j) { cin >> a; a--; tab[i].push(a); } int comp = (n * (n - 1LL)) / 2LL; // debug(comp); int ans = 0; while (comp) { bool f = 0; for (int i = 0; i < n; ++i) { if (vis[i] == idx || tab[i].empty()) continue; int adv = tab[i].front(); if (vis[adv] != idx && !tab[adv].empty() && tab[adv].front() == i) { tab[i].pop(); tab[adv].pop(); vis[adv] = vis[i] = idx; f = 1; comp--; } if (!comp) break; } if (!f) break; ans++, idx++; } if (!comp) cout << ans; else cout << -1; return 0; } // long long // array bounds // special cases // binary search
insert
0
0
0
1
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fs first #define sc second #define mk make_pair typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int INF = 1e9 + 1; const ll LINF = 1e17; const int MAKS = 1010; int n, x, day, num[MAKS][MAKS], cnt, in[MAKS * MAKS]; pii pos[MAKS * MAKS]; vector<int> lst, lst2, adj[MAKS * MAKS]; bool boo[MAKS]; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { num[i][j] = cnt; pos[cnt] = mk(i, j); cnt++; } } int last = -1; for (int i = 1; i <= n; i++) { for (int j = 0; j < n - 1; j++) { cin >> x; int a = min(i, x); int b = max(i, x); int p = num[a][b]; if (j != 0) { adj[last].pb(p); in[p]++; } last = p; } } for (int i = 0; i < cnt; i++) { if (in[i] == 0) lst.pb(i); } while (!lst.empty()) { day++; for (int now : lst) { // printf("%d %d %d\n", pos[now].fs, pos[now].sc, //now); boo[now] = true; for (int next : adj[now]) { in[next]--; if (in[next] == 0) lst2.pb(next); } } lst.clear(); for (int now : lst2) lst.pb(now); lst2.clear(); } for (int i = 0; i < cnt; i++) { if (!boo[i]) { // printf("%d\n", i); printf("-1"); return 0; } } cout << day; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fs first #define sc second #define mk make_pair typedef long long ll; typedef long double ld; typedef pair<int, int> pii; const int INF = 1e9 + 1; const ll LINF = 1e17; const int MAKS = 1010; int n, x, day, num[MAKS][MAKS], cnt, in[MAKS * MAKS]; pii pos[MAKS * MAKS]; vector<int> lst, lst2, adj[MAKS * MAKS]; bool boo[MAKS * MAKS]; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { num[i][j] = cnt; pos[cnt] = mk(i, j); cnt++; } } int last = -1; for (int i = 1; i <= n; i++) { for (int j = 0; j < n - 1; j++) { cin >> x; int a = min(i, x); int b = max(i, x); int p = num[a][b]; if (j != 0) { adj[last].pb(p); in[p]++; } last = p; } } for (int i = 0; i < cnt; i++) { if (in[i] == 0) lst.pb(i); } while (!lst.empty()) { day++; for (int now : lst) { // printf("%d %d %d\n", pos[now].fs, pos[now].sc, //now); boo[now] = true; for (int next : adj[now]) { in[next]--; if (in[next] == 0) lst2.pb(next); } } lst.clear(); for (int now : lst2) lst.pb(now); lst2.clear(); } for (int i = 0; i < cnt; i++) { if (!boo[i]) { // printf("%d\n", i); printf("-1"); return 0; } } cout << day; }
replace
15
16
15
16
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) // 有向グラフでの閉路検出 // O(V+E) const int NMAX = 1001010; vector<int> G[NMAX]; vector<int> status(NMAX, 0); vector<int> dp(NMAX, -1); // 0:New, 1:Active, 2:Finished // Activeは行きがけ順序、Finishedは帰り掛け順序 bool IsAcyclicDFS(int v) { status[v] = 1; for (auto to : G[v]) { if (status[to] == 1) return false; else if (status[to] == 0) { if (IsAcyclicDFS(to) == false) return false; } } status[v] = 2; return true; } bool IsAcyclic() { for (int i = 0; i < NMAX; ++i) status[i] = 0; for (int i = 0; i < NMAX; ++i) { if (status[i] == 0) { if (IsAcyclicDFS(i) == false) return false; } } return true; } int rec(int now) { if (dp[now] != -1) return dp[now]; int res = 1; for (auto to : G[now]) { chmax(res, rec(to) + 1); } return dp[now] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1, 0)); for (int i = 0; i < n; ++i) for (int j = 0; j < n - 1; ++j) { cin >> a[i][j]; --a[i][j]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 2; ++j) { int s1 = i, t1 = a[i][j]; int s2 = i, t2 = a[i][j + 1]; if (s1 > t1) swap(s1, t1); if (s2 > t2) swap(s2, t2); G[1000 * s1 + t1].push_back(1000 * s2 + t2); } } if (IsAcyclic() == false) { cout << -1 << endl; } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { chmax(ans, rec(1000 * i + j)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) // 有向グラフでの閉路検出 // O(V+E) const int NMAX = 1001010; vector<int> G[NMAX]; vector<int> status(NMAX, 0); vector<int> dp(NMAX, -1); // 0:New, 1:Active, 2:Finished // Activeは行きがけ順序、Finishedは帰り掛け順序 bool IsAcyclicDFS(int v) { status[v] = 1; for (auto to : G[v]) { if (status[to] == 1) return false; else if (status[to] == 0) { if (IsAcyclicDFS(to) == false) return false; } } status[v] = 2; return true; } bool IsAcyclic() { for (int i = 0; i < NMAX; ++i) status[i] = 0; for (int i = 0; i < NMAX; ++i) { if (status[i] == 0) { if (IsAcyclicDFS(i) == false) return false; } } return true; } int rec(int now) { if (dp[now] != -1) return dp[now]; int res = 1; for (auto to : G[now]) { chmax(res, rec(to) + 1); } return dp[now] = res; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1, 0)); for (int i = 0; i < n; ++i) for (int j = 0; j < n - 1; ++j) { cin >> a[i][j]; --a[i][j]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 2; ++j) { int s1 = i, t1 = a[i][j]; int s2 = i, t2 = a[i][j + 1]; if (s1 > t1) swap(s1, t1); if (s2 > t2) swap(s2, t2); G[1000 * s1 + t1].push_back(1000 * s2 + t2); } } if (IsAcyclic() == false) { cout << -1 << endl; return 0; } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { chmax(ans, rec(1000 * i + j)); } } cout << ans << endl; return 0; }
insert
140
140
140
141
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { long n; cin >> n; auto idx = [&](int x, int y) { int t = min(x, y); int s = max(x, y); return t * n + s; }; vector<vector<long>> a(n, vector<long>(n - 1)); for (auto &x : a) for (auto &y : x) cin >> y, y--; vector<int> out(n * n); vector<vector<int>> G(n * n), rG(n * n); vector<long> dis(n * n); for (int i = 0; i < n; i++) for (int j = 0; j < n - 1 - 1; j++) { int s = idx(i, a[i][j]); int t = idx(i, a[i][j + 1]); G[s].push_back(t); rG[t].push_back(s); out[s]++; } queue<int> q; for (int i = 0; i < n * n; i++) if (!out[i]) q.push(i); while (!q.empty()) { int i = q.front(); q.pop(); long r = -1; for (int f : G[i]) r = max(r, dis[f]); dis[i] = r + 1; for (int p : rG[i]) if (!--out[p]) q.push(p); } for (int i = 0; i < n * n; i++) if (out[i]) { cout << -1 << endl; return 1; } long r = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) r = max(r, dis[idx(i, j)]); cout << r + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { long n; cin >> n; auto idx = [&](int x, int y) { int t = min(x, y); int s = max(x, y); return t * n + s; }; vector<vector<long>> a(n, vector<long>(n - 1)); for (auto &x : a) for (auto &y : x) cin >> y, y--; vector<int> out(n * n); vector<vector<int>> G(n * n), rG(n * n); vector<long> dis(n * n); for (int i = 0; i < n; i++) for (int j = 0; j < n - 1 - 1; j++) { int s = idx(i, a[i][j]); int t = idx(i, a[i][j + 1]); G[s].push_back(t); rG[t].push_back(s); out[s]++; } queue<int> q; for (int i = 0; i < n * n; i++) if (!out[i]) q.push(i); while (!q.empty()) { int i = q.front(); q.pop(); long r = -1; for (int f : G[i]) r = max(r, dis[f]); dis[i] = r + 1; for (int p : rG[i]) if (!--out[p]) q.push(p); } for (int i = 0; i < n * n; i++) if (out[i]) { cout << -1 << endl; return 0; } long r = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) r = max(r, dis[idx(i, j)]); cout << r + 1 << endl; return 0; }
replace
54
55
54
55
0
p02925
C++
Time Limit Exceeded
#include <iostream> #include <queue> using namespace std; queue<int> q[1000]; int n, a, i, j; int d[1000]; int c = 0, t = 0, now, l; bool f; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n - 1; j++) { scanf("%d", &a); q[i].push(a - 1); } d[i] = -1; } l = n * (n - 1) / 2; while (true) { f = false; for (i = 0; i < n - 1; i++) { if (!q[i].empty()) { now = q[i].front(); if (d[i] != c && d[now] != c && !q[now].empty() && q[now].front() == i) { q[i].pop(), q[now].pop(); d[i] = c, d[now] = c; f = true; t++; } } } c++; if (!f) { cout << "-1" << endl; return 0; } if (t == l) { cout << c << endl; return 0; } } }
#include <iostream> #include <queue> using namespace std; queue<int> q[1000]; int n, a, i, j; int d[1000]; int c = 0, t = 0, now, l; bool f; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n - 1; j++) { scanf("%d", &a); q[i].push(a - 1); } d[i] = -1; } l = n * (n - 1) / 2; while (true) { f = false; for (i = 0; i < n - 1; i++) { if (!q[i].empty()) { now = q[i].front(); if (!q[now].empty() && q[now].front() == i) { if (d[i] == c || d[now] == c) continue; q[i].pop(), q[now].pop(); d[i] = c, d[now] = c; f = true; t++; } } } c++; if (!f) { cout << "-1" << endl; return 0; } if (t == l) { cout << c << endl; return 0; } } }
replace
26
28
26
29
TLE
p02925
C++
Runtime Error
#include <algorithm> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); i++) #define rrep(i, a, b) for (auto i = (a); i > (b); i--) #define all(v) (v).begin(), (v).end() #define print(v) \ { \ for (auto x : v) \ cout << x << ' '; \ cout << endl; \ } #define printn(v, n) \ { \ for (int _i = 0; _i < n; _i++) \ cout << *(v + _i) << ' '; \ cout << endl; \ } typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; const int MAXN = 1e3 + 20; int n; int a[MAXN][MAXN]; int get_node(int x, int y) { if (x > y) swap(x, y); return x * n + y; } vector<int> graph[MAXN]; int tin[MAXN * MAXN], tout[MAXN * MAXN], t; vector<int> res; bool dfs(int node) { tin[node] = t++; for (auto to : graph[node]) { if (tout[to]) continue; if (tin[to]) return false; else { bool tmp = dfs(to); if (!tmp) return false; } } tout[node] = t++; res.push_back(node); return true; } bool tp_sort() { memset(tin, 0, sizeof(tin)); memset(tout, 0, sizeof(tout)); t = 1; for (int i = 0; i < n; i++) { int node = get_node(i, a[i][0]); if (!tin[node]) { bool tmp = dfs(node); if (!tmp) return false; } } reverse(res.begin(), res.end()); return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; if (j) graph[get_node(i, a[i][j - 1])].push_back(get_node(i, a[i][j])); } } int valid = tp_sort(); if (!valid) { cout << -1 << endl; return 0; } vector<int> cur(n, 1); for (int i : res) { int x = i / n, y = i % n; int d = max(cur[x], cur[y]); cur[x] = d + 1; cur[y] = d + 1; } cout << *max_element(cur.begin(), cur.end()) - 1 << endl; // print(res); return 0; }
#include <algorithm> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define rep(i, a, b) for (auto i = (a); i < (b); i++) #define rrep(i, a, b) for (auto i = (a); i > (b); i--) #define all(v) (v).begin(), (v).end() #define print(v) \ { \ for (auto x : v) \ cout << x << ' '; \ cout << endl; \ } #define printn(v, n) \ { \ for (int _i = 0; _i < n; _i++) \ cout << *(v + _i) << ' '; \ cout << endl; \ } typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; const int MAXN = 1e3 + 20; int n; int a[MAXN][MAXN]; int get_node(int x, int y) { if (x > y) swap(x, y); return x * n + y; } vector<int> graph[MAXN * MAXN]; int tin[MAXN * MAXN], tout[MAXN * MAXN], t; vector<int> res; bool dfs(int node) { tin[node] = t++; for (auto to : graph[node]) { if (tout[to]) continue; if (tin[to]) return false; else { bool tmp = dfs(to); if (!tmp) return false; } } tout[node] = t++; res.push_back(node); return true; } bool tp_sort() { memset(tin, 0, sizeof(tin)); memset(tout, 0, sizeof(tout)); t = 1; for (int i = 0; i < n; i++) { int node = get_node(i, a[i][0]); if (!tin[node]) { bool tmp = dfs(node); if (!tmp) return false; } } reverse(res.begin(), res.end()); return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; if (j) graph[get_node(i, a[i][j - 1])].push_back(get_node(i, a[i][j])); } } int valid = tp_sort(); if (!valid) { cout << -1 << endl; return 0; } vector<int> cur(n, 1); for (int i : res) { int x = i / n, y = i % n; int d = max(cur[x], cur[y]); cur[x] = d + 1; cur[y] = d + 1; } cout << *max_element(cur.begin(), cur.end()) - 1 << endl; // print(res); return 0; }
replace
60
61
60
61
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, e) for (int i = a; i < e; ++i) using LL = long long; struct TopologicalSort { vector<vector<int>> E; TopologicalSort(int N) { E.resize(N); } void add_edge(int a, int b) { E[a].push_back(b); } bool visit(int v, vector<int> &order, vector<int> &color) { color[v] = 1; for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false; if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; } bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n); for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false; reverse(order.begin(), order.end()); return true; } }; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; 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; } vector<int> E[101010]; int dp[101010]; int main() { int N; cin >> N; // 閉路検出+DAGの最長パス using PII = pair<int, int>; int c = 0; map<PII, int> id; { FOR(i, 0, N) { FOR(j, i + 1, N) { id[PII(i, j)] = c++; } } } TopologicalSort G(c); FOR(i, 0, N) { vector<int> b(N - 1); FOR(j, 0, N - 1) { cin >> b[j]; b[j]--; } FOR(j, 0, N - 2) { int from = id[i < b[j] ? PII(i, b[j]) : PII(b[j], i)]; int to = id[i < b[j + 1] ? PII(i, b[j + 1]) : PII(b[j + 1], i)]; G.add_edge(from, to); E[from].push_back(to); } } vector<int> ord; bool ok = G.sort(ord); if (!ok) { cout << "-1" << endl; return 0; } else { fore(cu, ord) fore(to, E[cu]) chmax(dp[to], dp[cu] + 1); int ans = 0; rep(i, 0, c) chmax(ans, dp[i]); cout << ans + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, e) for (int i = a; i < e; ++i) using LL = long long; struct TopologicalSort { vector<vector<int>> E; TopologicalSort(int N) { E.resize(N); } void add_edge(int a, int b) { E[a].push_back(b); } bool visit(int v, vector<int> &order, vector<int> &color) { color[v] = 1; for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false; if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; } bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n); for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false; reverse(order.begin(), order.end()); return true; } }; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; 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; } vector<int> E[1000 * 500]; int dp[1000 * 500]; int main() { int N; cin >> N; // 閉路検出+DAGの最長パス using PII = pair<int, int>; int c = 0; map<PII, int> id; { FOR(i, 0, N) { FOR(j, i + 1, N) { id[PII(i, j)] = c++; } } } TopologicalSort G(c); FOR(i, 0, N) { vector<int> b(N - 1); FOR(j, 0, N - 1) { cin >> b[j]; b[j]--; } FOR(j, 0, N - 2) { int from = id[i < b[j] ? PII(i, b[j]) : PII(b[j], i)]; int to = id[i < b[j + 1] ? PII(i, b[j + 1]) : PII(b[j + 1], i)]; G.add_edge(from, to); E[from].push_back(to); } } vector<int> ord; bool ok = G.sort(ord); if (!ok) { cout << "-1" << endl; return 0; } else { fore(cu, ord) fore(to, E[cu]) chmax(dp[to], dp[cu] + 1); int ans = 0; rep(i, 0, c) chmax(ans, dp[i]); cout << ans + 1 << endl; } }
replace
54
56
54
56
0
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f, maxn = 1e3 + 1; int a[maxn][maxn], nowp[maxn], vis[maxn], n; int main() { int i, j, k, ans = 0; cin >> n; for (i = 1; i <= n; i++) { nowp[i] = 1; for (j = 1; j <= n - 1; j++) cin >> a[i][j]; } int cnt = 1; while (1) { bool f = false; for (i = 1; i <= n; i++) { if (nowp[i] >= n || vis[i] == cnt) continue; j = a[i][nowp[i]]; if (vis[j] != cnt && nowp[j] < n && a[j][nowp[j]] == i) { nowp[j]++; nowp[i]++; vis[i] = vis[j] = cnt; f = true; } } if (!f) break; cnt++; } for (int i = 1; i <= n; i++) { if (nowp[i] != n) { cout << -1; return 0; } } cout << cnt - 1; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f, maxn = 1e3 + 50; int a[maxn][maxn], nowp[maxn], vis[maxn], n; int main() { int i, j, k, ans = 0; cin >> n; for (i = 1; i <= n; i++) { nowp[i] = 1; for (j = 1; j <= n - 1; j++) cin >> a[i][j]; } int cnt = 1; while (1) { bool f = false; for (i = 1; i <= n; i++) { if (nowp[i] >= n || vis[i] == cnt) continue; j = a[i][nowp[i]]; if (vis[j] != cnt && nowp[j] < n && a[j][nowp[j]] == i) { nowp[j]++; nowp[i]++; vis[i] = vis[j] = cnt; f = true; } } if (!f) break; cnt++; } for (int i = 1; i <= n; i++) { if (nowp[i] != n) { cout << -1; return 0; } } cout << cnt - 1; return 0; }
replace
14
15
14
15
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; vector<vector<int>> vec; vector<vector<int>> now; bool dfs(int i) { int ii = now.at(i).at(0); if (ii == -1) { return false; } if (now.at(ii).at(0) == i) { return true; } else { return false; } } int main() { int N; cin >> N; vec.resize(N, vector<int>(N - 1)); now.resize(N, vector<int>(0)); vector<int> enemy(N); for (int i = 0; i < N; i++) { // O(N^2) for (int j = 0; j < N - 1; j++) { int a; cin >> a; vec.at(i).at(j) = a - 1; if (j == 0) { now.at(i).push_back(a - 1); } } enemy.at(i) = 0; } int count = 0; int day = 0; // countはN(N-1)/2まで while (count < N * (N - 1) / 2) { // O(N^3) unordered_map<int, int> mp; int memo = count; day++; for (int i = 0; i < N; i++) { // cout<<count<<" "<<i<<endl; if (dfs(i) && mp[i] == 0 && mp[now.at(i).at(0)] == 0 && now.at(i).at(0) != -1) { count++; int a = i; int b = now.at(i).at(0); enemy.at(a)++; enemy.at(b)++; mp[a] = 1; mp[b] = 1; // cout<<a<<" "<<b<<" "<<enemy.at(a)<<" "<<enemy.at(b)<<endl; if (enemy.at(a) < N - 1) { // cout<<a<<" "<<b<<" "<<enemy.at(a)<<endl; now.at(a).at(0) = vec.at(a).at(enemy.at(a)); } else { now.at(a).at(0) = -1; } if (enemy.at(b) != N - 1) { // cout<<" "<<a<<" "<<b<<" "<<enemy.at(b)<<endl; now.at(b).at(0) = vec.at(b).at(enemy.at(b)); } else { now.at(b).at(0) = -1; } } } if (memo == count) { cout << -1 << endl; return 0; } mp.clear(); } cout << day << endl; }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> vec; vector<vector<int>> now; bool dfs(int i) { int ii = now.at(i).at(0); if (ii == -1) { return false; } if (now.at(ii).at(0) == i) { return true; } else { return false; } } int main() { int N; cin >> N; vec.resize(N, vector<int>(N - 1)); now.resize(N, vector<int>(0)); vector<int> enemy(N); for (int i = 0; i < N; i++) { // O(N^2) for (int j = 0; j < N - 1; j++) { int a; cin >> a; vec.at(i).at(j) = a - 1; if (j == 0) { now.at(i).push_back(a - 1); } } enemy.at(i) = 0; } long long count = 0; long long day = 0; // countはN(N-1)/2まで while (count < N * (N - 1) / 2) { // O(N^3) unordered_map<int, int> mp; int memo = count; day++; for (int i = 0; i < N; i++) { // cout<<count<<" "<<i<<endl; if (dfs(i) && mp[i] == 0 && mp[now.at(i).at(0)] == 0 && now.at(i).at(0) != -1) { count++; int a = i; int b = now.at(i).at(0); enemy.at(a)++; enemy.at(b)++; mp[a] = 1; mp[b] = 1; // cout<<a<<" "<<b<<" "<<enemy.at(a)<<" "<<enemy.at(b)<<endl; if (enemy.at(a) < N - 1) { // cout<<a<<" "<<b<<" "<<enemy.at(a)<<endl; now.at(a).at(0) = vec.at(a).at(enemy.at(a)); } else { now.at(a).at(0) = -1; } if (enemy.at(b) != N - 1) { // cout<<" "<<a<<" "<<b<<" "<<enemy.at(b)<<endl; now.at(b).at(0) = vec.at(b).at(enemy.at(b)); } else { now.at(b).at(0) = -1; } } } if (memo == count) { cout << -1 << endl; return 0; } mp.clear(); } cout << day << endl; }
replace
32
34
32
34
TLE
p02925
C++
Time Limit Exceeded
/* _____ ____ _____ ___ ___ __ ___ |_ _/ __ \_ _| |__ \ / _ \/_ |/ _ \ | || | | || | ) | | | || | (_) | | || | | || | / /| | | || |\__, | _| || |__| || |_ / /_| |_| || | / / |_____\____/_____| |____|\___/ |_| /_/ */ #include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (long long i = (a); i > (b); i--) #define a(x) (x.begin(), x.end()) #define ar(x) (x.rbegin(), x.rend()) #define pb push_back #define Pb() pop_back() #define ll long long int #define ull unsigned long long int #define pii pair<int, int> #define pll pair<ll, ll> #define sc scanf #define scin(x) sc("%d", &(x)) #define scln(x) sc("%lld", &(x)) #define pf prllf #define ms(a, b) memset(a, b, sizeof(a)) #define mp make_pair #define db double #define EPS 10E-10 #define ff first #define ss second #define sqr(x) (x) * (x) #define vi vector<int> #define vl vector<ll> #define vii vector<vector<int>> #define vll vector<vector<ll>> #define DBG pf("HI\n") #define MOD 1000000007 #define CIN \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++) #define CASE(t) prllf("Case %d: ", t) #define CASEL(t) prllf("Case %d:\n", t) #define intlimit 2147483647 #define longlimit 9223372036854775807 #define infinity (1 << 28) #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a) * (b) / gcd(a, b)) #define PI 2 * acos(0.0) using namespace std; int main() { CIN; int n; cin >> n; vector<queue<int>> v(n); rep(i, 0, n) rep(j, 0, n - 1) { int zg; cin >> zg; v[i].push(zg); } int coco = 0; while (1) { set<int> s; rep(i, 0, n) { if (v[i].empty()) continue; int b = v[i].front() - 1; if (v[b].front() == i + 1 && !s.count(i) && !s.count(b)) { v[i].pop(); v[b].pop(); s.insert(i); s.insert(b); } } coco++; int sos = 0; rep(i, 0, n) { if (!v[i].empty()) { sos++; } // cout << last[i] << " "; } // cout << endl; if (!sos) break; if (s.size() == 0) { cout << -1; return 0; } } cout << coco; }
/* _____ ____ _____ ___ ___ __ ___ |_ _/ __ \_ _| |__ \ / _ \/_ |/ _ \ | || | | || | ) | | | || | (_) | | || | | || | / /| | | || |\__, | _| || |__| || |_ / /_| |_| || | / / |_____\____/_____| |____|\___/ |_| /_/ */ #include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (long long i = (a); i > (b); i--) #define a(x) (x.begin(), x.end()) #define ar(x) (x.rbegin(), x.rend()) #define pb push_back #define Pb() pop_back() #define ll long long int #define ull unsigned long long int #define pii pair<int, int> #define pll pair<ll, ll> #define sc scanf #define scin(x) sc("%d", &(x)) #define scln(x) sc("%lld", &(x)) #define pf prllf #define ms(a, b) memset(a, b, sizeof(a)) #define mp make_pair #define db double #define EPS 10E-10 #define ff first #define ss second #define sqr(x) (x) * (x) #define vi vector<int> #define vl vector<ll> #define vii vector<vector<int>> #define vll vector<vector<ll>> #define DBG pf("HI\n") #define MOD 1000000007 #define CIN \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++) #define CASE(t) prllf("Case %d: ", t) #define CASEL(t) prllf("Case %d:\n", t) #define intlimit 2147483647 #define longlimit 9223372036854775807 #define infinity (1 << 28) #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a) * (b) / gcd(a, b)) #define PI 2 * acos(0.0) using namespace std; int main() { CIN; int n; cin >> n; vector<queue<int>> v(n); rep(i, 0, n) rep(j, 0, n - 1) { int zg; cin >> zg; v[i].push(zg); } int coco = 0; while (1) { set<int> s; rep(i, 0, n) { if (v[i].empty()) continue; int b = v[i].front() - 1; if (v[b].front() == i + 1 && !s.count(i) && !s.count(b)) { v[i].pop(); v[b].pop(); s.insert(i); s.insert(b); } } coco++; int sos = 0; rep(i, 0, n) { if (!v[i].empty()) { sos++; } // cout << last[i] << " "; } // cout << endl; if (!sos) break; if (s.size() == 0) { cout << -1; return 0; } if (coco > 100000) { cout << 500 * 999; return 0; } } cout << coco; }
insert
89
89
89
93
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define debug(n) cerr << #n << ':' << n << endl; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T, class U> using umap = unordered_map<T, U>; using ll = long long; ll n; vector<vector<ll>> a; umap<ll, vector<ll>> to; int main() { const ll MAXN = 1010; const ll MAXV = MAXN * MAXN * 0.5; a.resize(n, vector<ll>(n - 1)); ll tbl[MAXN][MAXN] = {}; ll v = 0; rep(i, n) { rep(j, n) { if (i < j) tbl[i][j] = v++; } } auto id = [&](ll i, ll j) { if (i > j) swap(i, j); return tbl[i][j]; }; rep(i, n) { rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; if (j) to[id(i, a[i][j - 1])].push_back(id(i, a[i][j])); } } ll dist[MAXV] = {}; set<ll> vst, fin; auto dfs = [&](ll i, auto func) -> ll { if (vst.count(i) == 1) { if (fin.count(i) == 0) return -1; return dist[i]; } vst.insert(i); ll ret = 1; for (auto &e : to[i]) { ll d = func(e, func); if (d == -1) return -1; else ret = max(ret, d + 1); } fin.insert(i); dist[i] = ret; return dist[i]; }; ll ans = 1; rep(i, MAXV) { ll val = dfs(i, dfs); if (val == -1) { ans = -1; break; } ans = max(ans, val); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define debug(n) cerr << #n << ':' << n << endl; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T, class U> using umap = unordered_map<T, U>; using ll = long long; ll n; vector<vector<ll>> a; umap<ll, vector<ll>> to; int main() { cin >> n; const ll MAXN = 1010; const ll MAXV = MAXN * MAXN * 0.5; a.resize(n, vector<ll>(n - 1)); ll tbl[MAXN][MAXN] = {}; ll v = 0; rep(i, n) { rep(j, n) { if (i < j) tbl[i][j] = v++; } } auto id = [&](ll i, ll j) { if (i > j) swap(i, j); return tbl[i][j]; }; rep(i, n) { rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; if (j) to[id(i, a[i][j - 1])].push_back(id(i, a[i][j])); } } ll dist[MAXV] = {}; set<ll> vst, fin; auto dfs = [&](ll i, auto func) -> ll { if (vst.count(i) == 1) { if (fin.count(i) == 0) return -1; return dist[i]; } vst.insert(i); ll ret = 1; for (auto &e : to[i]) { ll d = func(e, func); if (d == -1) return -1; else ret = max(ret, d + 1); } fin.insert(i); dist[i] = ret; return dist[i]; }; ll ans = 1; rep(i, MAXV) { ll val = dfs(i, dfs); if (val == -1) { ans = -1; break; } ans = max(ans, val); } cout << ans << endl; return 0; }
insert
14
14
14
15
-11
p02925
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) #define pb push_back #define eb emplace_back using ll = long long; static const ll mod = 1e9 + 7; static const ll INF = 1LL << 50; using namespace std; #define maxv 1000 * (1000 - 1) / 2 + 1 int dp[1005]; vector<vector<int>> graph; vector<int> visited, cal; int dfs(int v) { // 最長経路 if (visited[v]) { if (!cal[v]) return -1; return dp[v]; } visited[v] = true; dp[v] = 1; for (auto x : graph[v]) { int res = dfs(x); if (res == -1) return -1; dp[v] = max(dp[v], res + 1); } cal[v] = 1; return dp[v]; // トポロジカルソートはメモ化再帰と相性がよい } signed main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; } int v = 0; vector<vector<int>> id(n, vector<int>(n)); graph.resize(maxv); rep(i, n) rep(j, n) { if (i < j) id[i][j] = v++; } rep(i, n) { rep(j, n - 1) { if (i < a[i][j]) a[i][j] = id[i][a[i][j]]; else a[i][j] = id[a[i][j]][i]; } rep(j, n - 2) graph[a[i][j + 1]].pb( a[i][j]); // 前後関係は同じ選手のスケジュールに依存 } int ans = 0; visited.resize(maxv, 0); cal.resize(maxv, 0); rep(i, v) { int res = dfs(i); if (res == -1) { // resが-1のとき、ループがあり実現不可 cout << -1 << endl; return 0; } ans = max(ans, res); } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) #define pb push_back #define eb emplace_back using ll = long long; static const ll mod = 1e9 + 7; static const ll INF = 1LL << 50; using namespace std; #define maxv 1000 * (1000 - 1) / 2 + 1 int dp[maxv]; vector<vector<int>> graph; vector<int> visited, cal; int dfs(int v) { // 最長経路 if (visited[v]) { if (!cal[v]) return -1; return dp[v]; } visited[v] = true; dp[v] = 1; for (auto x : graph[v]) { int res = dfs(x); if (res == -1) return -1; dp[v] = max(dp[v], res + 1); } cal[v] = 1; return dp[v]; // トポロジカルソートはメモ化再帰と相性がよい } signed main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; } int v = 0; vector<vector<int>> id(n, vector<int>(n)); graph.resize(maxv); rep(i, n) rep(j, n) { if (i < j) id[i][j] = v++; } rep(i, n) { rep(j, n - 1) { if (i < a[i][j]) a[i][j] = id[i][a[i][j]]; else a[i][j] = id[a[i][j]][i]; } rep(j, n - 2) graph[a[i][j + 1]].pb( a[i][j]); // 前後関係は同じ選手のスケジュールに依存 } int ans = 0; visited.resize(maxv, 0); cal.resize(maxv, 0); rep(i, v) { int res = dfs(i); if (res == -1) { // resが-1のとき、ループがあり実現不可 cout << -1 << endl; return 0; } ans = max(ans, res); } cout << ans << endl; return 0; }
replace
24
25
24
25
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = 1e9; vector<int> edge[500000]; int deg[500000]; int d[500000]; int main() { int n; cin >> n; int a[n][n - 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n - 2; j++) { int from = max(i, a[i][j]) * n + min(i, a[i][j]); int to = max(i, a[i][j + 1]) * n + min(i, a[i][j + 1]); edge[from].push_back(to); deg[to]++; } } queue<int> q; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i == j) continue; int id = max(i, j) * n + min(i, j); if (deg[id] == 0) { q.push(id); d[id] = 1; } } } vector<int> toposo; int ans = -1; while (q.size()) { int x = q.front(); q.pop(); toposo.push_back(x); ans = max(ans, d[x]); for (int y : edge[x]) { deg[y]--; if (deg[y] == 0) { q.push(y); d[y] = d[x] + 1; } } } if (toposo.size() != n * (n - 1) / 2) { cout << -1 << endl; } else { cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = 1e9; vector<int> edge[1000000]; int deg[1000000]; int d[1000000]; int main() { int n; cin >> n; int a[n][n - 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n - 2; j++) { int from = max(i, a[i][j]) * n + min(i, a[i][j]); int to = max(i, a[i][j + 1]) * n + min(i, a[i][j + 1]); edge[from].push_back(to); deg[to]++; } } queue<int> q; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i == j) continue; int id = max(i, j) * n + min(i, j); if (deg[id] == 0) { q.push(id); d[id] = 1; } } } vector<int> toposo; int ans = -1; while (q.size()) { int x = q.front(); q.pop(); toposo.push_back(x); ans = max(ans, d[x]); for (int y : edge[x]) { deg[y]--; if (deg[y] == 0) { q.push(y); d[y] = d[x] + 1; } } } if (toposo.size() != n * (n - 1) / 2) { cout << -1 << endl; } else { cout << ans << endl; } }
replace
6
9
6
9
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define pb push_back using namespace std; const int N = 1005; void fast() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, a[N][N]; queue<int> q[N]; vi v, e; bool check() { for (int i = 0; i < n; i++) { if (!q[i].empty()) { return false; } } return true; } int main() { fast(); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n - 1; j++) cin >> a[i][j], q[i].push(a[i][j] - 1); for (int i = 0; i < n; i++) v.pb(i); for (int ans = 1;; ans++) { e.clear(); vector<bool> use(n); if (check()) { cout << ans - 1 << endl; return 0; } bool ok = false; for (int k = 0; k < v.size(); k++) { int i = v[i]; if (!use[i] && !q[i].empty()) { int nxt = q[i].front(); if (!use[nxt] && !q[nxt].empty() && q[nxt].front() == i) { ok = true; use[nxt] = true; use[i] = true; q[nxt].pop(); q[i].pop(); e.pb(nxt); e.pb(i); } } } if (!ok) { cout << "-1"; return 0; } v = e; } return 0; }
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define pb push_back using namespace std; const int N = 1005; void fast() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, a[N][N]; queue<int> q[N]; vi v, e; bool check() { for (int i = 0; i < n; i++) { if (!q[i].empty()) { return false; } } return true; } int main() { fast(); cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n - 1; j++) cin >> a[i][j], q[i].push(a[i][j] - 1); for (int i = 0; i < n; i++) v.pb(i); for (int ans = 1;; ans++) { e.clear(); vector<bool> use(n); if (check()) { cout << ans - 1 << endl; return 0; } bool ok = false; for (int k = 0; k < v.size(); k++) { int i = v[k]; if (!use[i] && !q[i].empty()) { int nxt = q[i].front(); if (!use[nxt] && !q[nxt].empty() && q[nxt].front() == i) { ok = true; use[nxt] = true; use[i] = true; q[nxt].pop(); q[i].pop(); e.pb(nxt); e.pb(i); } } } if (!ok) { cout << "-1"; return 0; } v = e; } return 0; }
replace
43
44
43
44
-11
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(I, A, B) for (int I = int(A); I < int(B); I++) int main() { cin.tie(0); ios::sync_with_stdio(false); clock_t start = clock(); int N, ans = 0; cin >> N; vector<int> A[N + 1]; FOR(i, 1, N + 1) { A[i].resize(N); FOR(j, 1, N) { cin >> A[i][j]; } } bool game = true; vector<int> ind(N + 1, 1); // A[i][ind[i]] vector<int> nextOPP(N + 1, -1); vector<int> rest; FOR(i, 1, N + 1) rest.push_back(i); while (game) { game = false; ans++; vector<int> restt; for (int i : rest) nextOPP[i] = -1; for (int i : rest) { if (ind[i] >= N) continue; nextOPP[i] = A[i][ind[i]]; if (nextOPP[nextOPP[i]] == i) { game = true; ind[i]++; ind[nextOPP[i]]++; } } for (int i : rest) if (ind[i] < N) restt.push_back(i); rest = restt; clock_t end = clock(); double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0; if (time >= 1800.0) { cout << N * (N - 1) / 2 << endl; } } bool allok = true; FOR(i, 1, N + 1) if (ind[i] < N) allok = false; if (allok) { cout << ans - 1 << endl; } else { cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; #define FOR(I, A, B) for (int I = int(A); I < int(B); I++) int main() { cin.tie(0); ios::sync_with_stdio(false); clock_t start = clock(); int N, ans = 0; cin >> N; vector<int> A[N + 1]; FOR(i, 1, N + 1) { A[i].resize(N); FOR(j, 1, N) { cin >> A[i][j]; } } bool game = true; vector<int> ind(N + 1, 1); // A[i][ind[i]] vector<int> nextOPP(N + 1, -1); vector<int> rest; FOR(i, 1, N + 1) rest.push_back(i); while (game) { game = false; ans++; vector<int> restt; for (int i : rest) nextOPP[i] = -1; for (int i : rest) { if (ind[i] >= N) continue; nextOPP[i] = A[i][ind[i]]; if (nextOPP[nextOPP[i]] == i) { game = true; ind[i]++; ind[nextOPP[i]]++; } } for (int i : rest) if (ind[i] < N) restt.push_back(i); rest = restt; clock_t end = clock(); double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0; if (time >= 1800.0) { cout << N * (N - 1) / 2 << endl; return 0; } } bool allok = true; FOR(i, 1, N + 1) if (ind[i] < N) allok = false; if (allok) { cout << ans - 1 << endl; } else { cout << -1 << endl; } }
insert
48
48
48
49
TLE
p02925
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; const int MAXN = 1000; const int MAXV = MAXN * (MAXN - 1) / 2; vector<vector<int>> to(MAXV); vector<int> dp(MAXV); vector<bool> visited(MAXV, false); vector<bool> calculated(MAXV, false); int dfs(int v) { // cout << "dfs" << v << dp[v] << endl; if (visited[v]) { if (!calculated[v]) { // find roop! return -1; } // return dp[v]; } if (calculated[v]) return dp[v]; visited[v] = true; int res = 1; for (int p : to[v]) { int tmp = dfs(p); if (tmp == -1) return -1; res = max(res, tmp + 1); } dp[v] = res; calculated[v] = true; return dp[v]; } int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) { rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; } } // 試合組み合わせにidをつける vector<vector<int>> id(n, vector<int>(n - 1)); int matchId = 0; rep(i, n) { for (int j = i + 1; j < n; j++) { id[i][j] = matchId++; } } rep(i, n) { for (int j = 0; j < i; j++) { id[i][j] = id[j][i]; } } /* rep(i,n){ rep(j,n){ printf("%d%c", id[i][j], j==n-1? '\n':' '); } } */ rep(i, n) { rep(j, n - 1) { int from = a[i][j]; a[i][j] = id[i][from]; // printf("%d%c", a[i][j], j==n-2? '\n':' '); } } rep(i, n) { rep(j, n - 2) { to[a[i][j]].push_back(a[i][j + 1]); } } /* rep(i,matchId){ rep(j,to[i].size()){ printf("%d:%d%c", i,to[i][j], j==to[i].size()-1? '\n':' '); } } */ rep(i, matchId) { int tmp = dfs(i); if (tmp == -1) { puts("-1"); return 0; } } int res = 0; rep(i, dp.size()) res = max(res, dp[i]); cout << res << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const int MAXN = 1000; const int MAXV = MAXN * (MAXN - 1) / 2; vector<vector<int>> to(MAXV); vector<int> dp(MAXV); vector<bool> visited(MAXV, false); vector<bool> calculated(MAXV, false); int dfs(int v) { // cout << "dfs" << v << dp[v] << endl; if (visited[v]) { if (!calculated[v]) { // find roop! return -1; } // return dp[v]; } if (calculated[v]) return dp[v]; visited[v] = true; int res = 1; for (int p : to[v]) { int tmp = dfs(p); if (tmp == -1) return -1; res = max(res, tmp + 1); } dp[v] = res; calculated[v] = true; return dp[v]; } int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) { rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; } } // 試合組み合わせにidをつける vector<vector<int>> id(n, vector<int>(n)); int matchId = 0; rep(i, n) { for (int j = i + 1; j < n; j++) { id[i][j] = matchId++; } } rep(i, n) { for (int j = 0; j < i; j++) { id[i][j] = id[j][i]; } } /* rep(i,n){ rep(j,n){ printf("%d%c", id[i][j], j==n-1? '\n':' '); } } */ rep(i, n) { rep(j, n - 1) { int from = a[i][j]; a[i][j] = id[i][from]; // printf("%d%c", a[i][j], j==n-2? '\n':' '); } } rep(i, n) { rep(j, n - 2) { to[a[i][j]].push_back(a[i][j + 1]); } } /* rep(i,matchId){ rep(j,to[i].size()){ printf("%d:%d%c", i,to[i][j], j==to[i].size()-1? '\n':' '); } } */ rep(i, matchId) { int tmp = dfs(i); if (tmp == -1) { puts("-1"); return 0; } } int res = 0; rep(i, dp.size()) res = max(res, dp[i]); cout << res << endl; return 0; }
replace
52
53
52
53
0
p02925
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vvi a(n, vi(n - 1)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { cin >> a[i][j]; a[i][j]--; } } vi idx(n, 0); // vi to_ch(n, 0); // iota(to_ch.begin(), to_ch.end(), 0); set<int> to_check; for (int i = 0; i < n; ++i) { to_check.insert(i); } int days = 0; int used = 0; while (used < n * (n - 1)) { days++; set<int> temp; for (auto &aidx : to_check) { int tag = a[aidx][idx[aidx]]; if (a[tag][idx[tag]] == aidx && temp.count(aidx) == 0) { // idx[aidx]++; // idx[tag]++; temp.insert(aidx); temp.insert(tag); used += 2; } } if (temp.size() == 0) { cout << -1 << "\n"; return 0; } else { for (auto &t : temp) { idx[t]++; } to_check = temp; } } cout << days << "\n"; return 0; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vvi a(n, vi(n - 1)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { cin >> a[i][j]; a[i][j]--; } } vi idx(n, 0); // vi to_ch(n, 0); // iota(to_ch.begin(), to_ch.end(), 0); set<int> to_check; for (int i = 0; i < n; ++i) { to_check.insert(i); } int days = 0; int used = 0; while (used < n * (n - 1)) { days++; set<int> temp; for (auto &aidx : to_check) { if (idx[aidx] >= n - 1 || idx[a[aidx][idx[aidx]]] >= n - 1) continue; int tag = a[aidx][idx[aidx]]; if (a[tag][idx[tag]] == aidx && temp.count(aidx) == 0) { // idx[aidx]++; // idx[tag]++; temp.insert(aidx); temp.insert(tag); used += 2; } } if (temp.size() == 0) { cout << -1 << "\n"; return 0; } else { for (auto &t : temp) { idx[t]++; } to_check = temp; } } cout << days << "\n"; return 0; }
insert
48
48
48
50
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n, a[N][N], ans = 0, nxt[N], ptr[N]; int readInt() { char c; int ans = 0; while (1) { c = getchar(); if (c == ' ' || c == '\n') return ans; ans = (ans << 3) + (ans << 1) + (c - '0'); } } int main() { int n; n = readInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) { a[i][j] = readInt(); } ptr[i] = 1; } while (1) { bool ok = true; // if (ans >= 50000) return !printf("%d", -1); for (int i = 1; i <= n; i++) { if (ptr[i] <= n - 1) { ok = false; break; } } if (ok) break; ans++; memset(nxt, 0, sizeof(nxt)); // for (int i = 1; i <= n; i++) ptr2[i] = ptr[i]; ok = true; for (int i = 1; i <= n; i++) { if (ptr[i] >= n) continue; int cur = a[i][ptr[i]]; // cout << i << " " << cur << " " << ans << "\n"; // if (nxt[cur] && nxt[cur] != i) continue; // if (nxt[i]) continue; if (nxt[i] || nxt[cur]) { // ptr[cur]++; continue; } if (ptr[cur] >= n) continue; if (a[cur][ptr[cur]] != i) continue; nxt[cur] = i; nxt[i] = cur; ptr[i]++; ptr[cur]++; ok = false; } if (ok) return !printf("%d", -1); } printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n, a[N][N], ans = 0, nxt[N], ptr[N]; int readInt() { char c; int ans = 0; while (1) { c = getchar(); if (c == ' ' || c == '\n') return ans; ans = (ans << 3) + (ans << 1) + (c - '0'); } } int main() { int n; n = readInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) { a[i][j] = readInt(); } ptr[i] = 1; } while (1) { bool ok = true; if (ans >= 50000) return !printf("%d", n * (n - 1) / 2); for (int i = 1; i <= n; i++) { if (ptr[i] <= n - 1) { ok = false; break; } } if (ok) break; ans++; memset(nxt, 0, sizeof(nxt)); // for (int i = 1; i <= n; i++) ptr2[i] = ptr[i]; ok = true; for (int i = 1; i <= n; i++) { if (ptr[i] >= n) continue; int cur = a[i][ptr[i]]; // cout << i << " " << cur << " " << ans << "\n"; // if (nxt[cur] && nxt[cur] != i) continue; // if (nxt[i]) continue; if (nxt[i] || nxt[cur]) { // ptr[cur]++; continue; } if (ptr[cur] >= n) continue; if (a[cur][ptr[cur]] != i) continue; nxt[cur] = i; nxt[i] = cur; ptr[i]++; ptr[cur]++; ok = false; } if (ok) return !printf("%d", -1); } printf("%d", ans); return 0; }
replace
32
33
32
34
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { int b; cin >> b; a.at(i).at(j) = b - 1; } } double start_time = clock(); vector<int> game(n); int game_num = 0; int max_game_num = n * (n - 1) / 2; int answer = 0; while (true) { bool played_one_game = false; vector<int> aite(n); for (int i = 0; i < n; i++) { if (game.at(i) == n - 1) { aite.at(i) = -1; continue; } aite.at(i) = a.at(i).at(game.at(i)); } for (int i = 0; i < n; i++) { int teki = aite.at(i); if (teki == -1) continue; if (aite.at(teki) == i && i < teki) { game.at(i)++; game.at(teki)++; game_num++; played_one_game = true; } } if (!played_one_game) { answer = -1; break; } answer++; bool finished = true; for (int i = 0; i < n; i++) { if (game.at(i) != n - 1) finished = false; } if (finished) break; double now_time = clock(); if ((now_time - start_time) / CLOCKS_PER_SEC >= 1.8) { answer = n * (n - 1) / 2; break; } } cout << answer << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { int b; cin >> b; a.at(i).at(j) = b - 1; } } double start_time = clock(); vector<int> game(n); int game_num = 0; int max_game_num = n * (n - 1) / 2; int answer = 0; while (true) { bool played_one_game = false; vector<int> aite(n); for (int i = 0; i < n; i++) { if (game.at(i) == n - 1) { aite.at(i) = -1; continue; } aite.at(i) = a.at(i).at(game.at(i)); } for (int i = 0; i < n; i++) { int teki = aite.at(i); if (teki == -1) continue; if (aite.at(teki) == i && i < teki) { game.at(i)++; game.at(teki)++; game_num++; played_one_game = true; } } if (!played_one_game) { answer = -1; break; } answer++; bool finished = true; for (int i = 0; i < n; i++) { if (game.at(i) != n - 1) finished = false; } if (finished) break; double now_time = clock(); if ((now_time - start_time) / CLOCKS_PER_SEC >= 1.5) { answer = n * (n - 1) / 2; break; } } cout << answer << endl; }
replace
65
66
65
66
TLE
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; // const int INF = (1 << 30) - 1; // const ll LL_INF = (1LL << 62) - 1; // const ll MOD = 1e9 + 7; int N; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; vector<vector<int>> A(N, vector<int>(N - 1)); for (int i = 0; i < N; i++) { for (int j = 0; j < N - 1; j++) { int tmp; cin >> tmp; tmp--; A[i][j] = tmp; } } int ans = 0; int done = 0; vector<int> cur(N, 0); unordered_map<int, int> mp; while (done != N) { bool proceed = false; for (int i = 0; i < N; i++) { if (mp[i] == (ans + 1) || cur[i] == (N - 1)) { continue; } int a = A[i][cur[i]]; if (mp[a] == (ans + 1) || cur[a] == (N - 1)) { continue; } int b = A[a][cur[a]]; if (b == i) { proceed = true; cur[i]++; cur[a]++; mp[i] = (ans + 1); mp[a] = (ans + 1); if (cur[i] == (N - 1)) { done++; } if (cur[a] == (N - 1)) { done++; } } } if (!proceed) { cout << -1 << endl; return 0; } ans++; if (ans > 50000) { cout << 499500 << endl; } } cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; // const int INF = (1 << 30) - 1; // const ll LL_INF = (1LL << 62) - 1; // const ll MOD = 1e9 + 7; int N; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; vector<vector<int>> A(N, vector<int>(N - 1)); for (int i = 0; i < N; i++) { for (int j = 0; j < N - 1; j++) { int tmp; cin >> tmp; tmp--; A[i][j] = tmp; } } int ans = 0; int done = 0; vector<int> cur(N, 0); unordered_map<int, int> mp; while (done != N) { bool proceed = false; for (int i = 0; i < N; i++) { if (mp[i] == (ans + 1) || cur[i] == (N - 1)) { continue; } int a = A[i][cur[i]]; if (mp[a] == (ans + 1) || cur[a] == (N - 1)) { continue; } int b = A[a][cur[a]]; if (b == i) { proceed = true; cur[i]++; cur[a]++; mp[i] = (ans + 1); mp[a] = (ans + 1); if (cur[i] == (N - 1)) { done++; } if (cur[a] == (N - 1)) { done++; } } } if (!proceed) { cout << -1 << endl; return 0; } ans++; if (ans > 50000) { cout << 499500 << endl; return 0; } } cout << ans << endl; return 0; }
insert
70
70
70
71
TLE
p02925
C++
Time Limit Exceeded
#include <cstdio> using namespace std; int n, m, i, j, k, a, b; int str[1005][1005], vis[1050], top[1050]; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j < n; j++) { scanf("%d", &str[i][j]); } } for (i = 1; i <= n; i++) { top[i] = 1; } int day = 1; while (1) { bool con = false; for (i = 1; i <= n; i++) { if (vis[i] == day || top[i] >= n) continue; int v = str[i][top[i]]; if (vis[v] != day && str[v][top[v]] == i) { top[v]++; top[i]++; vis[v] = day; vis[i] = day; con = true; } } if (!con) break; day++; } for (i = 1; i <= n; i++) { if (top[i] != n) break; } if (i != n + 1) printf("-1\n"); else printf("%d\n", day - 1); return 0; }
#include <cstdio> using namespace std; int n, m, i, j, k, a, b; int str[1050][1050], vis[1050], top[1050]; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j < n; j++) { scanf("%d", &str[i][j]); } } for (i = 1; i <= n; i++) { top[i] = 1; } int day = 1; while (1) { bool con = false; for (i = 1; i <= n; i++) { if (vis[i] == day || top[i] >= n) continue; int v = str[i][top[i]]; if (vis[v] != day && str[v][top[v]] == i) { top[v]++; top[i]++; vis[v] = day; vis[i] = day; con = true; } } if (!con) break; day++; } for (i = 1; i <= n; i++) { if (top[i] != n) break; } if (i != n + 1) printf("-1\n"); else printf("%d\n", day - 1); return 0; }
replace
3
4
3
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() 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; } const int INF = (1 << 30) - 1; const ll LINF = (1LL << 60) - 1; int main() { int n; cin >> n; vector<queue<int>> A(n); int a; for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { cin >> a; A[i].push(a - 1); } } vector<int> v1, v2; for (int i = 0; i < n; ++i) { v1.push_back(i); } int res = 0; while (1) { bool fin = true; bool loop = true; vector<bool> checked(n, false); for (auto i : v1) { if (checked[i]) continue; if (A[i].empty()) { continue; } else { fin = false; } if (A[A[i].front()].front() == i && !checked[A[i].front()]) { checked[i] = true; checked[A[i].front()] = true; v2.push_back(i); v2.push_back(A[i].front()); A[A[i].front()].pop(); A[i].pop(); loop = false; } } if (fin) break; if (loop) { cout << -1 << endl; return 0; } v1.swap(v2); res++; } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() 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; } const int INF = (1 << 30) - 1; const ll LINF = (1LL << 60) - 1; int main() { int n; cin >> n; vector<queue<int>> A(n); int a; for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { cin >> a; A[i].push(a - 1); } } vector<int> v1, v2; for (int i = 0; i < n; ++i) { v1.push_back(i); } int res = 0; while (1) { bool fin = true; bool loop = true; vector<bool> checked(n, false); for (auto i : v1) { if (checked[i]) continue; if (A[i].empty()) { continue; } else { fin = false; } if (A[A[i].front()].front() == i && !checked[A[i].front()]) { checked[i] = true; checked[A[i].front()] = true; v2.push_back(i); v2.push_back(A[i].front()); A[A[i].front()].pop(); A[i].pop(); loop = false; } } if (fin) break; if (loop) { cout << -1 << endl; return 0; } v1.clear(); v1.swap(v2); res++; } cout << res << endl; }
insert
74
74
74
75
TLE
p02925
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = (1 << 30); const ll INFLL = (1ll << 60); const ll MOD = (ll)(1e9 + 7); #define l_ength size void mul_mod(ll &a, ll b) { a *= b; a %= MOD; } void add_mod(ll &a, ll b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } vector<P> g[1010][1010]; int dv[1010][1010], dp[1010][1010]; queue<P> q; vector<P> v; int main(void) { int n, m = 0, i, j, a, b, x, y, ans = 0; bool flag = false; cin >> n; for (i = 0; i < n; ++i) { cin >> a; --a; for (j = 2; j < n; ++j) { cin >> b; --b; g[max(i, a)][min(i, a)].push_back(P(max(i, b), min(i, b))); ++dv[max(i, b)][min(i, b)]; a = b; } } for (i = 0; i < n; ++i) { for (j = 0; j < i; ++j) { ++m; if (!dv[i][j]) { dp[i][j] = 1; q.push(P(i, j)); v.push_back(P(i, j)); } } } while (!q.empty()) { flag = true; a = q.front().first; b = q.front().second; q.pop(); for (i = (g[a][b].l_ength() - 1); i >= 0; --i) { x = g[a][b][i].first; y = g[a][b][i].second; --dv[x][y]; if (dv[x][y] < 0) { flag = false; break; } if (!dv[x][y]) { q.push(P(x, y)); v.push_back(P(x, y)); } } if (!flag) { break; } } if (!flag) { cout << "-1" << endl; return 0; } for (i = 0; i < m; ++i) { a = v[i].first; b = v[i].second; for (j = (g[a][b].l_ength() - 1); j >= 0; --j) { x = g[a][b][j].first; y = g[a][b][j].second; dp[x][y] = max(dp[x][y], dp[a][b] + 1); } ans = max(ans, dp[a][b]); } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = (1 << 30); const ll INFLL = (1ll << 60); const ll MOD = (ll)(1e9 + 7); #define l_ength size void mul_mod(ll &a, ll b) { a *= b; a %= MOD; } void add_mod(ll &a, ll b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } vector<P> g[1010][1010]; int dv[1010][1010], dp[1010][1010]; queue<P> q; vector<P> v; int main(void) { int n, m = 0, i, j, a, b, x, y, ans = 0; bool flag = false; cin >> n; for (i = 0; i < n; ++i) { cin >> a; --a; for (j = 2; j < n; ++j) { cin >> b; --b; g[max(i, a)][min(i, a)].push_back(P(max(i, b), min(i, b))); ++dv[max(i, b)][min(i, b)]; a = b; } } for (i = 0; i < n; ++i) { for (j = 0; j < i; ++j) { ++m; if (!dv[i][j]) { dp[i][j] = 1; q.push(P(i, j)); v.push_back(P(i, j)); } } } while (!q.empty()) { flag = true; a = q.front().first; b = q.front().second; q.pop(); for (i = (g[a][b].l_ength() - 1); i >= 0; --i) { x = g[a][b][i].first; y = g[a][b][i].second; --dv[x][y]; if (dv[x][y] < 0) { flag = false; break; } if (!dv[x][y]) { q.push(P(x, y)); v.push_back(P(x, y)); } } if (!flag) { break; } } if ((!flag) || (v.l_ength() != m)) { cout << "-1" << endl; return 0; } for (i = 0; i < m; ++i) { a = v[i].first; b = v[i].second; for (j = (g[a][b].l_ength() - 1); j >= 0; --j) { x = g[a][b][j].first; y = g[a][b][j].second; dp[x][y] = max(dp[x][y], dp[a][b] + 1); } ans = max(ans, dp[a][b]); } cout << ans << endl; return 0; }
replace
75
76
75
76
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int M = 1e3 + 10; int a[M][M]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { scanf("%d", &a[i][j]); } a[i][0] = 1; } int num = n * (n - 1) / 2; int flag1 = 0; int ans = 0; int i = 0; while (1) { ans++; int flag2 = 0; int used[M] = {0}; for (int j = 1; j <= n; j++) { long long k = a[j][0]; if (!used[j] && k != n) { long long t1 = a[j][k]; long long k2 = a[t1][0]; long long t2 = a[t1][k2]; if (t2 == j && !used[t1]) { a[t1][0]++; a[j][0]++; used[t1] = 1; used[j] = 1; flag2 = 1; i++; } } } if (!flag2) { flag1 = 1; break; } if (i == num) { break; } } if (!flag1) { printf("%d\n", ans); } else { printf("-1\n"); } }
#include <bits/stdc++.h> using namespace std; const int M = 2e3 + 10; int a[M][M]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { scanf("%d", &a[i][j]); } a[i][0] = 1; } int num = n * (n - 1) / 2; int flag1 = 0; int ans = 0; int i = 0; while (1) { ans++; int flag2 = 0; int used[M] = {0}; for (int j = 1; j <= n; j++) { long long k = a[j][0]; if (!used[j] && k != n) { long long t1 = a[j][k]; long long k2 = a[t1][0]; long long t2 = a[t1][k2]; if (t2 == j && !used[t1]) { a[t1][0]++; a[j][0]++; used[t1] = 1; used[j] = 1; flag2 = 1; i++; } } } if (!flag2) { flag1 = 1; break; } if (i == num) { break; } } if (!flag1) { printf("%d\n", ans); } else { printf("-1\n"); } }
replace
2
3
2
3
TLE
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <numeric> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) int main() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N - 1)); REP(i, N) { REP(j, N - 1) { cin >> A[i][j]; --A[i][j]; } } int ans = 0; vector<int> next_game(N, 0); REP(n, N * (N - 1) / 2) { vector<bool> in_game(N, false); REP(i, N) { if (next_game[i] == N - 1 or in_game[i]) continue; int enemy = A[i][next_game[i]]; if (in_game[enemy]) continue; if (A[enemy][next_game[enemy]] == i) { in_game[i] = in_game[enemy] = true; ++next_game[i], ++next_game[enemy]; } } ++ans; if (ans > 1e+7) { cout << N * (N - 1) / 2 << endl; return 0; } if (accumulate(next_game.begin(), next_game.end(), 0) == N * (N - 1)) break; } bool ok = true; REP(i, N) { if (next_game[i] != N - 1) { ok = false; break; } } if (ok) cout << ans << endl; else cout << -1 << endl; return 0; }
#include <algorithm> #include <iostream> #include <numeric> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) int main() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N - 1)); REP(i, N) { REP(j, N - 1) { cin >> A[i][j]; --A[i][j]; } } int ans = 0; vector<int> next_game(N, 0); REP(n, N * (N - 1) / 2) { vector<bool> in_game(N, false); REP(i, N) { if (next_game[i] == N - 1 or in_game[i]) continue; int enemy = A[i][next_game[i]]; if (in_game[enemy]) continue; if (A[enemy][next_game[enemy]] == i) { in_game[i] = in_game[enemy] = true; ++next_game[i], ++next_game[enemy]; } } ++ans; if (ans > 1e+5) { if (accumulate(next_game.begin(), next_game.end(), 0) > 100 * N) cout << N * (N - 1) / 2 << endl; else cout << -1 << endl; return 0; } if (accumulate(next_game.begin(), next_game.end(), 0) == N * (N - 1)) break; } bool ok = true; REP(i, N) { if (next_game[i] != N - 1) { ok = false; break; } } if (ok) cout << ans << endl; else cout << -1 << endl; return 0; }
replace
36
38
36
41
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep1(i, n) for (int i = 1; i <= (int)(n); ++i) #define rep11(i, n) for (int i = 1; i < (int)(n); ++i) #define repo(i, o, n) for (int i = o; i < (int)(n); ++i) #define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define pb(n) push_back(n) #define mp make_pair #define MOD 1000000007 #define INF LONG_LONG_MAX int n, ans[1000]; queue<int> que[1000]; signed main() { cin >> n; rep(i, n) rep(j, n - 1) { int a; cin >> a; que[i].push(a - 1); } bool f = true; while (f) { f = false; rep(i, n) { if (que[i].empty()) continue; int dif = que[i].front(); while (i == que[dif].front()) { ans[i] = ans[dif] = max(ans[i], ans[dif]) + 1; que[i].pop(); que[dif].pop(); if (que[i].size()) dif = que[i].front(); f = true; } } } int trueans = 0; rep(i, n) { if (que[i].size()) { cout << -1 << endl; return 0; } trueans = max(trueans, ans[i]); } cout << trueans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep1(i, n) for (int i = 1; i <= (int)(n); ++i) #define rep11(i, n) for (int i = 1; i < (int)(n); ++i) #define repo(i, o, n) for (int i = o; i < (int)(n); ++i) #define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define pb(n) push_back(n) #define mp make_pair #define MOD 1000000007 #define INF LONG_LONG_MAX int n, ans[1000]; queue<int> que[1000]; signed main() { cin >> n; rep(i, n) rep(j, n - 1) { int a; cin >> a; que[i].push(a - 1); } bool f = true; while (f) { f = false; rep(i, n) { if (que[i].empty()) continue; int dif = que[i].front(); while (i == que[dif].front()) { ans[i] = ans[dif] = max(ans[i], ans[dif]) + 1; que[i].pop(); que[dif].pop(); if (que[i].size()) dif = que[i].front(); else break; f = true; } } } int trueans = 0; rep(i, n) { if (que[i].size()) { cout << -1 << endl; return 0; } trueans = max(trueans, ans[i]); } cout << trueans << endl; }
insert
41
41
41
43
0
p02925
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <complex> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; int main() { ll N; cin >> N; vector<deque<ll>> tbl(N, deque<ll>(N - 1)); for (int i = 0; i < N; i++) { for (int j = 0; j < N - 1; j++) { cin >> tbl[i][j]; tbl[i][j]--; } // j } // j vector<P> que; auto check = [&](ll x) { if (tbl[x].size() == 0) return; ll xcompe = tbl[x].front(); if (x == tbl[xcompe].front()) { if (x > xcompe) swap(x, xcompe); que.push_back(P(x, xcompe)); } }; for (int i = 0; i < N; i++) { check(i); } ll ans = 0; ll games_done = 0; while (que.size() > 0) { ans++; vector<P> prevQ; que.erase(unique(que.begin(), que.end()), que.end()); swap(prevQ, que); for (auto &p : prevQ) { games_done++; tbl[p.first].pop_front(); tbl[p.second].pop_front(); } // p for (auto &p : prevQ) { check(p.first); check(p.second); } // p } // while if (games_done != N * (N - 1) / 2) ans = -1; cout << ans << endl; return 0; }
#include <algorithm> #include <cfloat> #include <complex> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; int main() { ll N; cin >> N; vector<deque<ll>> tbl(N, deque<ll>(N - 1)); for (int i = 0; i < N; i++) { for (int j = 0; j < N - 1; j++) { cin >> tbl[i][j]; tbl[i][j]--; } // j } // j vector<P> que; auto check = [&](ll x) { if (tbl[x].size() == 0) return; ll xcompe = tbl[x].front(); if (x == tbl[xcompe].front()) { if (x > xcompe) swap(x, xcompe); que.push_back(P(x, xcompe)); } }; for (int i = 0; i < N; i++) { check(i); } ll ans = 0; ll games_done = 0; while (que.size() > 0) { ans++; vector<P> prevQ; sort(que.begin(), que.end()); que.erase(unique(que.begin(), que.end()), que.end()); swap(prevQ, que); for (auto &p : prevQ) { games_done++; tbl[p.first].pop_front(); tbl[p.second].pop_front(); } // p for (auto &p : prevQ) { check(p.first); check(p.second); } // p } // while if (games_done != N * (N - 1) / 2) ans = -1; cout << ans << endl; return 0; }
insert
59
59
59
60
0
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; 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; } // グラフ関連 struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); // 最大フロー・最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(Graph &G, ll s, ll t) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } // 最短経路(負閉路検出) void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } // 最短経路(負の辺が含まれているとだめ) void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } // 最短経路問題(01bfs:0,1の辺のみ,O(|E|)) void bfs01(Graph &G, ll s, Array &d) { d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; deque<ll> deq; deq.push_front(s); while (deq.size() > 0) { ll v = deq.front(); // cout << v << endl; deq.pop_front(); for (auto e : G[v]) { if (d[v] + e.cap < d[e.to]) { d[e.to] = d[v] + e.cap; if (e.cap == 0) { deq.push_front(e.to); } else { deq.push_back(e.to); } } } } } // 最短経路(負の辺を含んでいてもOK) void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, Array &order) { // トポロジカルソートO(E+V) // 閉路検出にも使える int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; int num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unionSet(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; num--; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } int numSet() { return num; } }; template <class Abel> struct PotentialUnionFind { vector<int> par; vector<int> rank; vector<Abel> diff_weight; PotentialUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(int n = 1, Abel SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } Abel weight(int x) { root(x); return diff_weight[x]; } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y, Abel w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } Abel diff(int x, int y) { return weight(y) - weight(x); } }; class SumSegTree { private: int _sum(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { int s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 int s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: int n, height; vector<int> dat; // 初期化(_nは最大要素数) SumSegTree(int _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<int>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(int i, int x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 int sum(int a, int b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { // 区間[l,r)の最小値を持つk番目のノードを探索 if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // 完全に含む else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // i番目の値(0-indexed)をxに変更 void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 登りながら更新 i = (i - 1) / 2; // 親ノードのインデックス dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); // 子ノードの小さい方の値を代入 } } // 区間[a,b)の最小値の取得 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // nCr,nPr,modの逆元 class Combination { public: Array fact; // n! Array inv; // n!の逆元 ll mod; Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); REP(i, n + 1) { inv[i] = mod_inv(fact[i]); } } // modの逆元 ll mod_inv(ll x) { ll n = mod - 2; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { // 重複組み合わせ:n種類のものからr個取り出す組み合わせ return nCr(r + n - 1, r); } }; // 約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } // 素因数分解 void factoring(ll n, map<ll, ll> &mp) { for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { mp[i]++; n /= i; } } if (n != 1) mp[n]++; } // 最大公約数 ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd // 最小公倍数 ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } // 繰り返し自乗法 ll pow2(ll x, ll n) { // x^n 計算量O(logn) ll ans = 1ll; while (n > 0) { if ((n & 1) == 1) { ans = ans * x; } x = x * x; // 一周する度にx, x^2, x^4, x^8となる n >>= 1; // 桁をずらす n = n >> 1 } return ans; } ll pow2_m(ll x, ll n, ll mod) { // x^n 計算量O(logn) ll ans = 1ll; while (n > 0) { if ((n & 1) == 1) { ans = ans * x % mod; } x = x * x % mod; // 一周する度にx, x^2, x^4, x^8となる n >>= 1; // 桁をずらす n = n >> 1 } return ans; } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { // id[i] = distance(A.begin(), upper_bound(A.begin(), A.end(), // a[i]));//最長部分非減少列 id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } // memo // output : fixed << setprecision(n) <<小数点以下n桁出力 // setfill('0') << setw(12) <<num 0埋めで12文字出力 // func // lower_bound(all(a),x):vector aでx以上の要素のイテレータを返す // upper_bound(all(a,x)):vector aでxより大きい要素のイテレータを返す // setやmapの場合は専用関数あり // 調和関数はlogn! n/1+n/2+n/3+... ll dp[1000010]; ll dfs(Graph g, ll v) { // cout << v << endl; if (dp[v] != 0) return dp[v]; if (g[v].size() == 0) { dp[v] = 1; return dp[v]; } ll ans = 0; for (auto e : g[v]) { // cout << "b" << e.to << endl; chmax(ans, dfs(g, e.to)); } dp[v] = ans + 1; return dp[v]; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; Graph g(n * n); Array used(n * n, false); REP(i, n) { ll pre, now, a; cin >> a; a--; if (i < a) { pre = n * i + a; } else { pre = n * a + i; } REP(j, n - 2) { cin >> a; a--; if (i < a) { now = n * i + a; } else { now = n * a + i; } add_edge(g, pre, now, 1, false, 0); // cout << pre << " " << now << endl; pre = now; used[now] = true; } } ll ans = 0; Array order; if (tsort(g, order)) { REP(i, n) { // cout << "a" << i << endl; for (int j = i + 1; j < n; j++) { ll v = i * n + j; if (!used[v]) { chmax(ans, dfs(g, v)); } } } } else { cout << -1 << endl; return 0; } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define MOD 1000000007 #define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() typedef pair<ll, ll> P; const long long INF = 1LL << 60; typedef vector<ll> Array; typedef vector<Array> Matrix; 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; } // グラフ関連 struct Edge { // グラフ ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { // 最大フロー求める Ford-fulkerson G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); // 最大フロー・最小カットの場合逆辺は0にする } ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(Graph &G, ll s, ll t) { ll flow = 0; for (;;) { vector<bool> used(G.size()); REP(i, used.size()) used[i] = false; ll f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } // 最短経路(負閉路検出) void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|) d.resize(G.size()); negative.resize(G.size()); REP(i, d.size()) d[i] = INF; REP(i, d.size()) negative[i] = false; d[s] = 0; REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } REP(k, G.size() - 1) { REP(i, G.size()) { REP(j, G[i].size()) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } // 最短経路(負の辺が含まれているとだめ) void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|) d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(make_pair(0, s)); while (!q.empty()) { P a = q.top(); q.pop(); if (d[a.second] < a.first) continue; REP(i, G[a.second].size()) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } // 最短経路問題(01bfs:0,1の辺のみ,O(|E|)) void bfs01(Graph &G, ll s, Array &d) { d.resize(G.size()); REP(i, d.size()) d[i] = INF; d[s] = 0; deque<ll> deq; deq.push_front(s); while (deq.size() > 0) { ll v = deq.front(); // cout << v << endl; deq.pop_front(); for (auto e : G[v]) { if (d[v] + e.cap < d[e.to]) { d[e.to] = d[v] + e.cap; if (e.cap == 0) { deq.push_front(e.to); } else { deq.push_back(e.to); } } } } } // 最短経路(負の辺を含んでいてもOK) void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3) d.resize(G.size()); REP(i, d.size()) d[i].resize(G.size()); REP(i, d.size()) { REP(j, d[i].size()) { d[i][j] = INF; } } REP(i, G.size()) { REP(j, G[i].size()) { d[i][G[i][j].to] = G[i][j].cap; } } REP(i, G.size()) { REP(j, G.size()) { REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(Graph &graph, Array &order) { // トポロジカルソートO(E+V) // 閉路検出にも使える int n = graph.size(), k = 0; Array in(n); for (auto &es : graph) for (auto &e : es) in[e.to]++; priority_queue<ll, Array, greater<ll>> que; REP(i, n) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto &e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const Graph &g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const Graph &g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto &e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; class UnionFind { vector<int> data; int num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unionSet(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; num--; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } int numSet() { return num; } }; template <class Abel> struct PotentialUnionFind { vector<int> par; vector<int> rank; vector<Abel> diff_weight; PotentialUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(int n = 1, Abel SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } Abel weight(int x) { root(x); return diff_weight[x]; } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y, Abel w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } Abel diff(int x, int y) { return weight(y) - weight(x); } }; class SumSegTree { private: int _sum(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; // 交差しない if (a <= l && r <= b) return dat[k]; // a,l,r,bの順で完全に含まれる else { int s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 int s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return s1 + s2; } } public: int n, height; vector<int> dat; // 初期化(_nは最大要素数) SumSegTree(int _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<int>(2 * n - 1, 0); } // 場所i(0-indexed)にxを足す void add(int i, int x) { i += n - 1; // i番目の葉ノードへ dat[i] += x; while (i > 0) { // 下から上がっていく i = (i - 1) / 2; dat[i] += x; } } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 int sum(int a, int b) { return _sum(a, b, 0, 0, n); } }; class RmqTree { private: ll _find(ll a, ll b, ll k, ll l, ll r) { // 区間[l,r)の最小値を持つk番目のノードを探索 if (r <= a || b <= l) return INF; // 交差しない if (a <= l && r <= b) return dat[k]; // 完全に含む else { ll s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 ll s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return min(s1, s2); } } public: ll n, height; vector<ll> dat; // 初期化(_nは最大要素数) RmqTree(ll _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<ll>(2 * n - 1, INF); } // i番目の値(0-indexed)をxに変更 void update(ll i, ll x) { i += n - 1; // i番目の葉ノードへ dat[i] = x; while (i > 0) { // 登りながら更新 i = (i - 1) / 2; // 親ノードのインデックス dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); // 子ノードの小さい方の値を代入 } } // 区間[a,b)の最小値の取得 ll find(ll a, ll b) { return _find(a, b, 0, 0, n); } }; // nCr,nPr,modの逆元 class Combination { public: Array fact; // n! Array inv; // n!の逆元 ll mod; Combination(ll n, ll _mod) { mod = _mod; fact.resize(n + 1); fact[0] = 1; REP(i, n) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n + 1); REP(i, n + 1) { inv[i] = mod_inv(fact[i]); } } // modの逆元 ll mod_inv(ll x) { ll n = mod - 2; ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll nCr(ll n, ll r) { return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; } ll nPr(ll n, ll r) { return (fact[n] * inv[n - r]) % mod; } ll nHr(ll n, ll r) { // 重複組み合わせ:n種類のものからr個取り出す組み合わせ return nCr(r + n - 1, r); } }; // 約数 void divisor(ll n, vector<ll> &ret) { for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } // 素因数分解 void factoring(ll n, map<ll, ll> &mp) { for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { mp[i]++; n /= i; } } if (n != 1) mp[n]++; } // 最大公約数 ll gcd(ll m, ll n) { if (n == 0) return m; return gcd(n, m % n); } // gcd // 最小公倍数 ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } // 繰り返し自乗法 ll pow2(ll x, ll n) { // x^n 計算量O(logn) ll ans = 1ll; while (n > 0) { if ((n & 1) == 1) { ans = ans * x; } x = x * x; // 一周する度にx, x^2, x^4, x^8となる n >>= 1; // 桁をずらす n = n >> 1 } return ans; } ll pow2_m(ll x, ll n, ll mod) { // x^n 計算量O(logn) ll ans = 1ll; while (n > 0) { if ((n & 1) == 1) { ans = ans * x % mod; } x = x * x % mod; // 一周する度にx, x^2, x^4, x^8となる n >>= 1; // 桁をずらす n = n >> 1 } return ans; } vector<ll> lis_fast(const vector<ll> &a) { // 最長部分増加列 const ll n = a.size(); vector<ll> A(n, INT_MAX); vector<ll> id(n); for (int i = 0; i < n; ++i) { // id[i] = distance(A.begin(), upper_bound(A.begin(), A.end(), // a[i]));//最長部分非減少列 id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); A[id[i]] = a[i]; } ll m = *max_element(id.begin(), id.end()); vector<ll> b(m + 1); for (int i = n - 1; i >= 0; --i) if (id[i] == m) b[m--] = a[i]; return b; } // memo // output : fixed << setprecision(n) <<小数点以下n桁出力 // setfill('0') << setw(12) <<num 0埋めで12文字出力 // func // lower_bound(all(a),x):vector aでx以上の要素のイテレータを返す // upper_bound(all(a,x)):vector aでxより大きい要素のイテレータを返す // setやmapの場合は専用関数あり // 調和関数はlogn! n/1+n/2+n/3+... ll dp[1000010]; ll dfs(Graph &g, ll v) { // cout << v << endl; if (dp[v] != 0) return dp[v]; if (g[v].size() == 0) { dp[v] = 1; return dp[v]; } ll ans = 0; for (auto e : g[v]) { // cout << "b" << e.to << endl; chmax(ans, dfs(g, e.to)); } dp[v] = ans + 1; return dp[v]; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; Graph g(n * n); Array used(n * n, false); REP(i, n) { ll pre, now, a; cin >> a; a--; if (i < a) { pre = n * i + a; } else { pre = n * a + i; } REP(j, n - 2) { cin >> a; a--; if (i < a) { now = n * i + a; } else { now = n * a + i; } add_edge(g, pre, now, 1, false, 0); // cout << pre << " " << now << endl; pre = now; used[now] = true; } } ll ans = 0; Array order; if (tsort(g, order)) { REP(i, n) { // cout << "a" << i << endl; for (int j = i + 1; j < n; j++) { ll v = i * n + j; if (!used[v]) { chmax(ans, dfs(g, v)); } } } } else { cout << -1 << endl; return 0; } cout << ans << endl; return 0; }
replace
547
548
547
548
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define FI first #define SE second #define EPS 1e-9 #define ALL(a) a.begin(), a.end() #define SZ(a) int((a).size()) #define MS(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORE(i, a, b) for (int i = (a); i >= (b); i--) #define FORALL(it, a) \ for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define WHATIS(x) cout << #x << " is " << x << endl; #define ERROR(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } //__builtin_ffs(x) return 1 + index of least significant 1-bit of x //__builtin_clz(x) return number of leading zeros of x //__builtin_ctz(x) return number of trailing zeros of x using namespace std; using ll = long long; using ld = double; typedef pair<int, int> II; typedef pair<II, int> III; typedef complex<ld> cd; typedef vector<cd> vcd; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } const ll MODBASE = 1000000007LL; const int MAXN = 1010; const int MAXM = 200010; const int MAXK = 110; const int MAXQ = 200010; int n, pos[MAXN]; vector<int> a[MAXN], tmp; set<II> s; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n; FOR(i, 1, n) FOR(j, 1, n - 1) { int u; cin >> u; a[i].emplace_back(u); } FOR(i, 1, n) pos[i] = 0; FOR(i, 1, n) { int g = a[i][pos[i]]; int h = a[g][pos[g]]; if (h == i) { if (i < g) s.insert(II(i, g)); else s.insert(II(g, i)); } } // FORALL(it, s) cout << it->FI << ' ' << it->SE << endl; int cnt = 0; while (SZ(s)) { cnt++; FORALL(it, s) { int g = it->FI; int h = it->SE; pos[g]++; pos[h]++; tmp.emplace_back(g); tmp.emplace_back(h); } // FOR(i,1,n) cout << pos[i] << ' '; cout << endl; s.clear(); FOR(i, 0, SZ(tmp) - 1) { int p = tmp[i]; if (pos[p] >= n - 1) continue; int g = a[p][pos[p]]; if (pos[g] >= n - 1) continue; int h = a[g][pos[g]]; if (h == p) { if (p < g) s.insert(II(p, g)); else s.insert(II(g, p)); } } } FOR(i, 1, n) if (pos[i] < n - 1) { cout << -1; return 0; } cout << cnt; return 0; }
#include <bits/stdc++.h> #define FI first #define SE second #define EPS 1e-9 #define ALL(a) a.begin(), a.end() #define SZ(a) int((a).size()) #define MS(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORE(i, a, b) for (int i = (a); i >= (b); i--) #define FORALL(it, a) \ for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define WHATIS(x) cout << #x << " is " << x << endl; #define ERROR(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } //__builtin_ffs(x) return 1 + index of least significant 1-bit of x //__builtin_clz(x) return number of leading zeros of x //__builtin_ctz(x) return number of trailing zeros of x using namespace std; using ll = long long; using ld = double; typedef pair<int, int> II; typedef pair<II, int> III; typedef complex<ld> cd; typedef vector<cd> vcd; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } const ll MODBASE = 1000000007LL; const int MAXN = 1010; const int MAXM = 200010; const int MAXK = 110; const int MAXQ = 200010; int n, pos[MAXN]; vector<int> a[MAXN], tmp; set<II> s; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n; FOR(i, 1, n) FOR(j, 1, n - 1) { int u; cin >> u; a[i].emplace_back(u); } FOR(i, 1, n) pos[i] = 0; FOR(i, 1, n) { int g = a[i][pos[i]]; int h = a[g][pos[g]]; if (h == i) { if (i < g) s.insert(II(i, g)); else s.insert(II(g, i)); } } // FORALL(it, s) cout << it->FI << ' ' << it->SE << endl; int cnt = 0; while (SZ(s)) { cnt++; FORALL(it, s) { int g = it->FI; int h = it->SE; pos[g]++; pos[h]++; tmp.emplace_back(g); tmp.emplace_back(h); } // FOR(i,1,n) cout << pos[i] << ' '; cout << endl; s.clear(); FOR(i, 0, SZ(tmp) - 1) { int p = tmp[i]; if (pos[p] >= n - 1) continue; int g = a[p][pos[p]]; if (pos[g] >= n - 1) continue; int h = a[g][pos[g]]; if (h == p) { if (p < g) s.insert(II(p, g)); else s.insert(II(g, p)); } } tmp.clear(); } FOR(i, 1, n) if (pos[i] < n - 1) { cout << -1; return 0; } cout << cnt; return 0; }
insert
99
99
99
100
TLE
p02925
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rep2(i, a, b) for (int(i) = a; (i) < (b); (i)++) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long int; const int inf = 1001001000; const long long int Inf = 1001001001001001000; void print(vector<vector<int>> a) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[i].size(); j++) { cout << a[i][j] << " "; } cout << endl; } } void print(vector<vector<long long int>> a) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[i].size(); j++) { cout << a[i][j] << " "; } cout << endl; } } void print(vector<int> a) { int n = a.size(); for (int j = 0; j < n; j++) { if (j != n - 1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(vector<long long int> a) { int n = a.size(); for (int j = 0; j < n; j++) { if (j != n - 1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(set<int> a) { for (auto x : a) cout << x << " "; cout << endl; } int toid(int i, int j, vector<vector<int>> &id) { if (i > j) swap(i, j); return id[i][j]; } // 深さ優先探索のテンプレ.デフォルトでは参照渡ししたsetに探索済みの頂点が格納される. // graphは隣接リスト.vは探索開始地点.seenは空のセット int dfs(vector<vector<int>> &graph, int v, vector<int> &dp, vector<int> &calc) { if (dp[v] >= 0) { if (calc[v] == 0) return -1; return dp[v]; } dp[v] = 1; for (auto nxt : graph[v]) { int temp = dfs(graph, nxt, dp, calc); if (temp == -1) return -1; dp[v] = max(dp[v], temp + 1); } calc[v] = 1; return dp[v]; } int main() { int n; cin >> n; vector<vector<int>> p(n, vector<int>(n - 1)); vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) { rep(j, n - 1) { cin >> p[i][j]; p[i][j]--; } } int v = 0; vector<vector<int>> id(n, vector<int>(n - 1)); rep(i, n) { rep(j, n) { id[i][j] = v; v++; } } vector<vector<int>> graph(v); rep(i, n) { rep(j, n - 1) { a[i][j] = toid(i, p[i][j], id); } rep(j, n - 2) { graph[a[i][j]].push_back(a[i][j + 1]); } } vector<int> dp(v, -1); vector<int> calc(v, 0); int ans = 0; rep(i, v) { int temp = dfs(graph, i, dp, calc); if (temp == -1) { cout << -1 << endl; return 0; } ans = max(ans, temp); } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rep2(i, a, b) for (int(i) = a; (i) < (b); (i)++) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long int; const int inf = 1001001000; const long long int Inf = 1001001001001001000; void print(vector<vector<int>> a) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[i].size(); j++) { cout << a[i][j] << " "; } cout << endl; } } void print(vector<vector<long long int>> a) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[i].size(); j++) { cout << a[i][j] << " "; } cout << endl; } } void print(vector<int> a) { int n = a.size(); for (int j = 0; j < n; j++) { if (j != n - 1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(vector<long long int> a) { int n = a.size(); for (int j = 0; j < n; j++) { if (j != n - 1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(set<int> a) { for (auto x : a) cout << x << " "; cout << endl; } int toid(int i, int j, vector<vector<int>> &id) { if (i > j) swap(i, j); return id[i][j]; } // 深さ優先探索のテンプレ.デフォルトでは参照渡ししたsetに探索済みの頂点が格納される. // graphは隣接リスト.vは探索開始地点.seenは空のセット int dfs(vector<vector<int>> &graph, int v, vector<int> &dp, vector<int> &calc) { if (dp[v] >= 0) { if (calc[v] == 0) return -1; return dp[v]; } dp[v] = 1; for (auto nxt : graph[v]) { int temp = dfs(graph, nxt, dp, calc); if (temp == -1) return -1; dp[v] = max(dp[v], temp + 1); } calc[v] = 1; return dp[v]; } int main() { int n; cin >> n; vector<vector<int>> p(n, vector<int>(n - 1)); vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) { rep(j, n - 1) { cin >> p[i][j]; p[i][j]--; } } int v = 0; vector<vector<int>> id(n, vector<int>(n)); rep(i, n) { rep(j, n) { id[i][j] = v; v++; } } vector<vector<int>> graph(v); rep(i, n) { rep(j, n - 1) { a[i][j] = toid(i, p[i][j], id); } rep(j, n - 2) { graph[a[i][j]].push_back(a[i][j + 1]); } } vector<int> dp(v, -1); vector<int> calc(v, 0); int ans = 0; rep(i, v) { int temp = dfs(graph, i, dp, calc); if (temp == -1) { cout << -1 << endl; return 0; } ans = max(ans, temp); } cout << ans << endl; return 0; }
replace
107
108
107
108
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<vector<int>> a(n, vector<int>(n - 1)); for (auto &i : a) for (auto &j : i) { scanf("%d", &j); j--; } vector<int> outdeg(n * n + 5, 0); vector<vector<int>> adj(n * n + 5); vector<int> indeg(n * n + 5, 0); vector<vector<int>> cadj(n * n + 5); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { for (int k = j + 1; k < n - 1; k++) { int l1 = min(i, a[i][j]), l2 = max(i, a[i][j]); int p1 = min(i, a[i][k]), p2 = max(i, a[i][k]); adj[l1 * n + l2].push_back(p1 * n + p2); cadj[p1 * n + p2].push_back(l1 * n + l2); indeg[p1 * n + p2]++; outdeg[l1 * n + l2]++; } } } // int ind = -1; queue<int> q; for (int i = 0; i < (n * n); i++) { int sec = i % n, fir = i / n; if (fir >= sec) continue; if (indeg[i] == 0) { q.push(i); } } int vis = 0; while (!q.empty()) { int u = q.front(); q.pop(); vis++; for (int v : adj[u]) { indeg[v]--; if (indeg[v] == 0) q.push(v); } } if (vis != (n * (n - 1) / 2)) { printf("-1\n"); return 0; } vector<int> d(n * n + 5, 0); for (int i = 0; i < (n * n); i++) { int sec = i % n, fir = i / n; if (fir >= sec) continue; if (outdeg[i] == 0) { d[i] = 1; q.push(i); } } int cnt = 0; while (!q.empty()) { int u = q.front(); q.pop(); cnt++; for (int v : cadj[u]) { outdeg[v]--; d[v] = max(d[v], d[u] + 1); if (outdeg[v] == 0) q.push(v); } } // cerr << "cnt = " << cnt << endl; printf("%d\n", *max_element(d.begin(), d.end())); }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<vector<int>> a(n, vector<int>(n - 1)); for (auto &i : a) for (auto &j : i) { scanf("%d", &j); j--; } vector<int> outdeg(n * n + 5, 0); vector<vector<int>> adj(n * n + 5); vector<int> indeg(n * n + 5, 0); vector<vector<int>> cadj(n * n + 5); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { // for (int k=j+1; k<n-1; k++) { int k = j + 1; if (k >= (n - 1)) continue; int l1 = min(i, a[i][j]), l2 = max(i, a[i][j]); int p1 = min(i, a[i][k]), p2 = max(i, a[i][k]); adj[l1 * n + l2].push_back(p1 * n + p2); cadj[p1 * n + p2].push_back(l1 * n + l2); indeg[p1 * n + p2]++; outdeg[l1 * n + l2]++; // } } } // int ind = -1; queue<int> q; for (int i = 0; i < (n * n); i++) { int sec = i % n, fir = i / n; if (fir >= sec) continue; if (indeg[i] == 0) { q.push(i); } } int vis = 0; while (!q.empty()) { int u = q.front(); q.pop(); vis++; for (int v : adj[u]) { indeg[v]--; if (indeg[v] == 0) q.push(v); } } if (vis != (n * (n - 1) / 2)) { printf("-1\n"); return 0; } vector<int> d(n * n + 5, 0); for (int i = 0; i < (n * n); i++) { int sec = i % n, fir = i / n; if (fir >= sec) continue; if (outdeg[i] == 0) { d[i] = 1; q.push(i); } } int cnt = 0; while (!q.empty()) { int u = q.front(); q.pop(); cnt++; for (int v : cadj[u]) { outdeg[v]--; d[v] = max(d[v], d[u] + 1); if (outdeg[v] == 0) q.push(v); } } // cerr << "cnt = " << cnt << endl; printf("%d\n", *max_element(d.begin(), d.end())); }
replace
18
26
18
29
TLE
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <utility> #include <vector> #define WHITE 0 #define GRAY 1 #define BLACK 2 #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i) #define arep(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define all(x) (x).begin(), (x).end() using namespace std; typedef pair<int, int> P; const int MAX_N = 1010; int A[MAX_N][MAX_N]; int node_number[MAX_N][MAX_N]; vector<int> graph[MAX_N * MAX_N]; int state[MAX_N * MAX_N]; bool dfs(int node, vector<int> &tsorted_node) { if (state[node] == GRAY) return false; bool res = true; state[node] = GRAY; rep(i, 0, graph[node].size()) { res &= dfs(graph[node][i], tsorted_node); } tsorted_node.push_back(node); state[node] = BLACK; return res; } vector<int> tsort(int n) { bool is_ok = true; vector<int> res; rep(i, 0, n) { if (state[i] == BLACK) continue; is_ok &= dfs(i, res); } if (is_ok) { reverse(all(res)); return res; } else return vector<int>(0); } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; rep(i, 0, N) rep(j, 0, N - 1) { cin >> A[i][j]; A[i][j]--; } rep(i, 0, N) rep(j, 0, N) node_number[i][j] = -1; int number = 0; rep(i, 0, N) rep(j, 0, N - 2) { if (node_number[i][A[i][j]] < 0) node_number[i][A[i][j]] = node_number[A[i][j]][i] = number++; if (node_number[i][A[i][j + 1]] < 0) node_number[i][A[i][j + 1]] = node_number[A[i][j + 1]][i] = number++; graph[node_number[i][A[i][j]]].push_back(node_number[i][A[i][j + 1]]); } vector<int> nodes = tsort(N * (N - 1) / 2); if (nodes.size() == 0) { cout << -1 << endl; return 0; } vector<int> dist(nodes.size(), 0); rep(i, 0, nodes.size()) { rep(j, 0, graph[nodes[i]].size()) { dist[graph[nodes[i]][j]] = max(dist[graph[nodes[i]][j]], dist[nodes[i]] + 1); } } int ans = 0; rep(i, 0, dist.size()) { ans = max(ans, dist[i]); } ans++; cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <utility> #include <vector> #define WHITE 0 #define GRAY 1 #define BLACK 2 #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i) #define arep(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define all(x) (x).begin(), (x).end() using namespace std; typedef pair<int, int> P; const int MAX_N = 1010; int A[MAX_N][MAX_N]; int node_number[MAX_N][MAX_N]; vector<int> graph[MAX_N * MAX_N]; int state[MAX_N * MAX_N]; bool dfs(int node, vector<int> &tsorted_node) { if (state[node] == GRAY) return false; if (state[node] == BLACK) return true; bool res = true; state[node] = GRAY; rep(i, 0, graph[node].size()) { res &= dfs(graph[node][i], tsorted_node); } tsorted_node.push_back(node); state[node] = BLACK; return res; } vector<int> tsort(int n) { bool is_ok = true; vector<int> res; rep(i, 0, n) { if (state[i] == BLACK) continue; is_ok &= dfs(i, res); } if (is_ok) { reverse(all(res)); return res; } else return vector<int>(0); } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; rep(i, 0, N) rep(j, 0, N - 1) { cin >> A[i][j]; A[i][j]--; } rep(i, 0, N) rep(j, 0, N) node_number[i][j] = -1; int number = 0; rep(i, 0, N) rep(j, 0, N - 2) { if (node_number[i][A[i][j]] < 0) node_number[i][A[i][j]] = node_number[A[i][j]][i] = number++; if (node_number[i][A[i][j + 1]] < 0) node_number[i][A[i][j + 1]] = node_number[A[i][j + 1]][i] = number++; graph[node_number[i][A[i][j]]].push_back(node_number[i][A[i][j + 1]]); } vector<int> nodes = tsort(N * (N - 1) / 2); if (nodes.size() == 0) { cout << -1 << endl; return 0; } vector<int> dist(nodes.size(), 0); rep(i, 0, nodes.size()) { rep(j, 0, graph[nodes[i]].size()) { dist[graph[nodes[i]][j]] = max(dist[graph[nodes[i]][j]], dist[nodes[i]] + 1); } } int ans = 0; rep(i, 0, dist.size()) { ans = max(ans, dist[i]); } ans++; cout << ans << endl; return 0; }
insert
26
26
26
28
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1024, INF = 0x3f3f3f3f, M = 2e6 + 5; // const ll LNF = 0x3f3f3f3f3f3f3f3f; #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, r, l) for (int i = r; i >= l; i--) #define repe(i, u) for (int i = head[u]; i; i = nxt[i]) int n, a[N][N], vis[N]; bool flag; bool ok() { for (int i = 1; i <= n; i++) if (a[i][0] != n) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } for (int z = 0; z <= 0x3f3f3f3f; z++) { if (ok()) { cout << z << '\n'; return 0; } flag = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) { a[i][0]++; a[x][0]++; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { cout << -1 << '\n'; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1023, INF = 0x3f3f3f3f, M = 2e6 + 5; // const ll LNF = 0x3f3f3f3f3f3f3f3f; #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, r, l) for (int i = r; i >= l; i--) #define repe(i, u) for (int i = head[u]; i; i = nxt[i]) int n, a[N][N], vis[N]; bool flag; bool ok() { for (int i = 1; i <= n; i++) if (a[i][0] != n) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } for (int z = 0; z <= 0x3f3f3f3f; z++) { if (ok()) { cout << z << '\n'; return 0; } flag = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) { a[i][0]++; a[x][0]++; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { cout << -1 << '\n'; return 0; } } return 0; }
replace
3
4
3
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5 + 5, INF = 0x3f3f3f3f, M = 2e6 + 5; const ll LNF = 0x3f3f3f3f3f3f3f3f; #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, r, l) for (int i = r; i >= l; i--) #define repe(i, u) for (int i = head[u]; i; i = nxt[i]) int n, a[1025][1025], vis[1025]; bool flag; bool ok() { for (int i = 1; i <= n; i++) if (a[i][0] != n) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } for (int z = 0; z <= 0x3f3f3f3f; z++) { if (ok()) { cout << z << '\n'; return 0; } flag = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) { a[i][0]++; a[x][0]++; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { cout << -1 << '\n'; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, a[1025][1025], vis[1025]; bool flag; bool ok() { for (int i = 1; i <= n; i++) if (a[i][0] != n) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } for (int z = 0; z <= 0x3f3f3f3f; z++) { if (ok()) { cout << z << '\n'; return 0; } flag = 0; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) { a[i][0]++; a[x][0]++; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { cout << -1 << '\n'; return 0; } } return 0; }
replace
3
8
3
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[1024][1024], z; bool vis[1024], flag; bool ok() { for (int i = 1; i <= n; i++) if (a[i][0] != n) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (ok()) { cout << z << '\n'; return 0; } flag = 0; z++; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) { a[i][0]++; a[x][0]++; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { cout << -1 << '\n'; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[1025][1025], z; bool vis[1025], flag; bool ok() { for (int i = 1; i <= n; i++) if (a[i][0] != n) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (ok()) { cout << z << '\n'; return 0; } flag = 0; z++; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; i++) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) { a[i][0]++; a[x][0]++; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { cout << -1 << '\n'; return 0; } } return 0; }
replace
3
5
3
5
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[20000][20000], t = 0; bool flag[20000], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[11000][11000], t = 0; bool flag[11000], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
replace
2
4
2
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[8192][8192], t = 0; bool flag[8192], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[11000][11000], t = 0; bool flag[11000], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
replace
2
4
2
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[2049][2049], t = 0; bool flag[2049], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[1025][1025], t = 0; bool flag[1025], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
replace
2
4
2
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[6025][6025], t = 0; bool flag[6025], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[1025][1025], t = 0; bool flag[1025], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
replace
2
4
2
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[1024][1024], t = 0; bool flag[1024], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[1025][1025], t = 0; bool flag[1025], fl = 0; bool judge() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (judge()) { printf("%d\n", t); return 0; } fl = 0; memset(flag, 0, sizeof(flag)); ++t; for (int i = 1; i <= n; ++i) { if (flag[i] || a[i][0] == n) continue; int x = a[i][a[i][0]], y = a[x][a[x][0]]; if (!flag[x] && !flag[i] && i == y) { fl = 1; flag[x] = 1; flag[i] = 1; ++a[i][0]; ++a[x][0]; } } if (!fl) { printf("-1\n"); return 0; } } return 0; }
replace
2
4
2
4
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, a[1005][1005], z; bool vis[1005], flag; bool ok() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (ok()) { printf("%d\n", z); return 0; } flag = 0; z++; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; ++i) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) { ++a[i][0]; ++a[x][0]; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { printf("-1\n"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[10010][10010], z; bool vis[10010], flag; bool ok() { for (int i = 1; i <= n; ++i) if (a[i][0] != n) return 0; return 1; } int main() { // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n - 1; ++j) cin >> a[i][j]; a[i][0] = 1; } while (1) { if (ok()) { printf("%d\n", z); return 0; } flag = 0; z++; memset(vis, 0, sizeof vis); for (int i = 1; i <= n; ++i) { if (vis[i] || a[i][0] == n) continue; int x = a[i][a[i][0]]; if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) { ++a[i][0]; ++a[x][0]; vis[i] = vis[x] = 1; flag = 1; } } if (!flag) { printf("-1\n"); return 0; } } return 0; }
replace
3
5
3
5
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n, m) for (int i = n; i < (int)(m); i++) #define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; using namespace std; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, 0, n) rep(j, 0, n - 1) { cin >> a[i][j]; a[i][j]--; } int ans = 0; vector<int> now_index(n, 0); bool finish = false; while (true) { finish = true; vector<int> add(n, 0); rep(i, 0, n) { if (now_index[i] >= n - 1) continue; int now = a[i][now_index[i]]; if (a[now][now_index[now]] == i) { add[i]++; finish = false; } } rep(i, 0, n) { now_index[i] += add[i]; } if (finish) break; ans++; } rep(i, 0, n) { if (now_index[i] < n - 1) ans = -1; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n, m) for (int i = n; i < (int)(m); i++) #define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; using namespace std; int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, 0, n) rep(j, 0, n - 1) { cin >> a[i][j]; a[i][j]--; } int ans = 0; vector<int> now_index(n, 0); bool finish = false; while (true) { finish = true; vector<int> add(n, 0); rep(i, 0, n) { if (now_index[i] >= n - 1) continue; int now = a[i][now_index[i]]; if (a[now][now_index[now]] == i) { add[i]++; finish = false; } } rep(i, 0, n) { now_index[i] += add[i]; } if (finish) break; ans++; if (ans >= 100000) { cout << n * (n - 1) / 2 << endl; return 0; } } rep(i, 0, n) { if (now_index[i] < n - 1) ans = -1; } cout << ans << endl; }
insert
41
41
41
45
TLE
p02925
C++
Runtime Error
// #pragma GCC target("avx2") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす #include <algorithm> #include <bitset> #include <cassert> #include <deque> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <vector> using namespace std; const long long mod = 1000000007; const long long inf = mod * mod; const long long d2 = (mod + 1) / 2; const double EPS = 1e-10; const double INF = 1e+10; const double PI = acos(-1.0); const int C_SIZE = 3121000; namespace { long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a, int b) { if (a < b || b < 0) return 0; return fact[a] * finv[b] % mod * finv[a - b] % mod; } void init_C(int n) { fact[0] = finv[0] = inv[1] = 1; for (int i = 2; i < n; i++) { inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod; } for (int i = 1; i < n; i++) { fact[i] = fact[i - 1] * i % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long pw(long long a, long long b) { if (a < 0LL) return 0; if (b < 0LL) return 0; long long ret = 1; while (b) { if (b % 2) ret = ret * a % mod; a = a * a % mod; b /= 2; } return ret; } int ABS(int a) { return max(a, -a); } long long ABS(long long a) { return max(a, -a); } double ABS(double a) { return max(a, -a); } int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } } // namespace // ここから編集しろ int p[1100][1100]; int G[1100][1100]; vector<int> g[510000]; int v[510000]; int dfs(int a) { if (v[a] == -2) { return -2; } if (v[a] != -1) { return v[a]; } v[a] = -2; int ret = 0; for (int i = 0; i < g[a].size(); i++) { int tmp = dfs(g[a][i]); if (tmp == -2) return -2; ret = max(ret, tmp + 1); } return v[a] = ret; } int main() { int a; scanf("%d", &a); for (int i = 0; i < a; i++) { for (int j = 0; j < a - 1; j++) { scanf("%d", &p[i][j]); p[i][j]--; } } int sz = 0; for (int i = 0; i < a; i++) { for (int j = 1; j < a; j++) G[i][j] = sz++; } for (int i = 0; i < a; i++) { for (int j = 0; j < a - 2; j++) { int fi = G[min(i, p[i][j])][max(i, p[i][j])]; int se = G[min(i, p[i][j + 1])][max(i, p[i][j + 1])]; g[fi].push_back(se); } } for (int i = 0; i < sz; i++) v[i] = -1; int ret = 0; for (int i = 0; i < sz; i++) { if (v[i] == -1) { int val = dfs(i); if (val == -2) { printf("-1\n"); return 0; } ret = max(ret, val); } } printf("%d\n", ret + 1); }
// #pragma GCC target("avx2") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす #include <algorithm> #include <bitset> #include <cassert> #include <deque> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <vector> using namespace std; const long long mod = 1000000007; const long long inf = mod * mod; const long long d2 = (mod + 1) / 2; const double EPS = 1e-10; const double INF = 1e+10; const double PI = acos(-1.0); const int C_SIZE = 3121000; namespace { long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a, int b) { if (a < b || b < 0) return 0; return fact[a] * finv[b] % mod * finv[a - b] % mod; } void init_C(int n) { fact[0] = finv[0] = inv[1] = 1; for (int i = 2; i < n; i++) { inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod; } for (int i = 1; i < n; i++) { fact[i] = fact[i - 1] * i % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long pw(long long a, long long b) { if (a < 0LL) return 0; if (b < 0LL) return 0; long long ret = 1; while (b) { if (b % 2) ret = ret * a % mod; a = a * a % mod; b /= 2; } return ret; } int ABS(int a) { return max(a, -a); } long long ABS(long long a) { return max(a, -a); } double ABS(double a) { return max(a, -a); } int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } } // namespace // ここから編集しろ int p[1100][1100]; int G[1100][1100]; vector<int> g[510000]; int v[510000]; int dfs(int a) { if (v[a] == -2) { return -2; } if (v[a] != -1) { return v[a]; } v[a] = -2; int ret = 0; for (int i = 0; i < g[a].size(); i++) { int tmp = dfs(g[a][i]); if (tmp == -2) return -2; ret = max(ret, tmp + 1); } return v[a] = ret; } int main() { int a; scanf("%d", &a); for (int i = 0; i < a; i++) { for (int j = 0; j < a - 1; j++) { scanf("%d", &p[i][j]); p[i][j]--; } } int sz = 0; for (int i = 0; i < a; i++) { for (int j = i + 1; j < a; j++) G[i][j] = sz++; } for (int i = 0; i < a; i++) { for (int j = 0; j < a - 2; j++) { int fi = G[min(i, p[i][j])][max(i, p[i][j])]; int se = G[min(i, p[i][j + 1])][max(i, p[i][j + 1])]; g[fi].push_back(se); } } for (int i = 0; i < sz; i++) v[i] = -1; int ret = 0; for (int i = 0; i < sz; i++) { if (v[i] == -1) { int val = dfs(i); if (val == -2) { printf("-1\n"); return 0; } ret = max(ret, val); } } printf("%d\n", ret + 1); }
replace
98
99
98
99
-6
terminate called without an active exception
p02925
C++
Time Limit Exceeded
// #include <bits/stdc++.h> using namespace std; #define ll long long #define maxnn 2000 int n; int ry[maxnn][maxnn]; int ru[maxnn]; int d = 0; int mark[maxnn]; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { scanf("%d", &ry[i][j]); ru[i] = 1; } } while (1) { d++; for (int i = 1; i <= n; i++) { mark[i] = 0; } int tt = 0; int tr = 0; for (int i = 1; i <= n; i++) { if (ru[i] == n) { continue; } if (mark[i]) { continue; } if (ry[ry[i][ru[i]]][ru[ry[i][ru[i]]]] != i) { tt++; } else if (!mark[i] && !mark[ry[i][ru[i]]]) { ru[ry[i][ru[i]]]++; mark[i] = 1; mark[ry[i][ru[i]]] = 1; ru[i]++; } } for (int i = 1; i <= n; i++) if (ru[i] == n) tr++; if (tr == n) { cout << d; return 0; } if (n - tr == tt) { cout << -1; return 0; } } }
// #include <bits/stdc++.h> using namespace std; #define ll long long #define maxnn 2000 ll n; ll ry[maxnn][maxnn]; ll ru[maxnn]; ll d = 0; ll mark[maxnn]; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { scanf("%d", &ry[i][j]); ru[i] = 1; } } while (1) { d++; for (int i = 1; i <= n; i++) { mark[i] = 0; } int tt = 0; int tr = 0; for (int i = 1; i <= n; i++) { if (ru[i] == n) { continue; } if (mark[i]) { continue; } if (ry[ry[i][ru[i]]][ru[ry[i][ru[i]]]] != i) { tt++; } else if (!mark[i] && !mark[ry[i][ru[i]]]) { ru[ry[i][ru[i]]]++; mark[i] = 1; mark[ry[i][ru[i]]] = 1; ru[i]++; } } for (int i = 1; i <= n; i++) if (ru[i] == n) tr++; if (tr == n) { cout << d; return 0; } if (n - tr == tt) { cout << -1; return 0; } } }
replace
5
10
5
10
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<LL, LL> P; const LL mod = 1000000007; const LL LINF = 1LL << 60; const int INF = 1 << 30; int main() { LL n; cin >> n; vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; } } vector<int> c(n, 0); vector<int> x(n, 0); auto q = clock(); for (int i = 1; i <= n * (n - 1) / 2 + 1; i++) { if (clock() - q >= 1900000) { cout << n * (n - 1) / 2 << endl; return 0; } bool f = false; bool t = false; for (int j = 0; j < n; j++) { if (c[j] == n - 1) continue; t = true; int en = a[j][c[j]]; if (x[j] == i || x[en] == i) continue; if (c[en] == n - 1) continue; if (j == a[en][c[en]]) { c[j]++; c[en]++; x[j] = i; x[en] = i; f = true; } } if (!t) { cout << i - 1 << endl; return 0; } if (!f) { puts("-1"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long LL; typedef pair<LL, LL> P; const LL mod = 1000000007; const LL LINF = 1LL << 60; const int INF = 1 << 30; int main() { LL n; cin >> n; vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; } } vector<int> c(n, 0); vector<int> x(n, 0); auto q = clock(); for (int i = 1; i <= n * (n - 1) / 2 + 1; i++) { if (clock() - q >= 1000000) { cout << n * (n - 1) / 2 << endl; return 0; } bool f = false; bool t = false; for (int j = 0; j < n; j++) { if (c[j] == n - 1) continue; t = true; int en = a[j][c[j]]; if (x[j] == i || x[en] == i) continue; if (c[en] == n - 1) continue; if (j == a[en][c[en]]) { c[j]++; c[en]++; x[j] = i; x[en] = i; f = true; } } if (!t) { cout << i - 1 << endl; return 0; } if (!f) { puts("-1"); return 0; } } return 0; }
replace
29
30
29
30
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; using Pii = pair<int, int>; using Pll = pair<ll, ll>; #define REP(i, l, n) for (int i = (l), i##_len = (n); i < i##_len; ++i) #define ALL(x) (x).begin(), (x).end() #define pb push_back ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, 1, -1}; vector<int> chidx; int n; int check() { REP(i, 0, n) { if (chidx[i] != n - 1) { return 1; } } return 0; } int main() { cin >> n; chidx = vector<int>(n, 0); vector<vector<int>> lis(n, vector<int>(n - 1)); REP(i, 0, n) { REP(l, 0, n - 1) { cin >> lis[i][l]; lis[i][l]--; } } int ans = 0; while (check() == 1) { ans++; map<int, int> used; int f = 0; REP(i, 0, n) { if (chidx[i] == n - 1) { continue; } int to = lis[i][chidx[i]]; if (used[i] == 1 || used[to] == 1) { continue; } if (chidx[to] == n - 1) { cout << -1 << endl; return 0; } if (lis[to][chidx[to]] == i) { chidx[i]++; chidx[to]++; used[i] = 1; used[to] = 1; f = 1; } } if (f == 0) { cout << -1 << endl; return 0; } } cout << ans << endl; }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; using Pii = pair<int, int>; using Pll = pair<ll, ll>; #define REP(i, l, n) for (int i = (l), i##_len = (n); i < i##_len; ++i) #define ALL(x) (x).begin(), (x).end() #define pb push_back ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, 1, -1}; vector<int> chidx; int n; int check() { REP(i, 0, n) { if (chidx[i] != n - 1) { return 1; } } return 0; } int main() { cin >> n; chidx = vector<int>(n, 0); vector<vector<int>> lis(n, vector<int>(n - 1)); REP(i, 0, n) { REP(l, 0, n - 1) { cin >> lis[i][l]; lis[i][l]--; } } int ans = 0; while (check() == 1) { ans++; vector<int> used(n, 0); int f = 0; REP(i, 0, n) { if (chidx[i] == n - 1) { continue; } int to = lis[i][chidx[i]]; if (used[i] == 1 || used[to] == 1) { continue; } if (chidx[to] == n - 1) { cout << -1 << endl; return 0; } if (lis[to][chidx[to]] == i) { chidx[i]++; chidx[to]++; used[i] = 1; used[to] = 1; f = 1; } } if (f == 0) { cout << -1 << endl; return 0; } } cout << ans << endl; }
replace
63
64
63
64
TLE
p02925
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; const int INF = 1001001001; const long long LINF = 1001002003004005006ll; 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; } typedef long long ll; typedef pair<int, int> P; const int MAXN = 1005; const int MAXV = MAXN * (MAXN - 1) / 2; int id[MAXN][MAXN]; int calc_v(int a, int b) { if (a > b) swap(a, b); return id[a][b]; } vector<int> to[MAXV]; vector<int> d(MAXV, -1); vector<int> calced(MAXV, -1); bool dag = 1; int dfs(int v) { if (d[v] != -1) return d[v]; d[v] = 0; calced[v] = 0; for (int u : to[v]) { if (calced[u] == 0) { dag = 0; continue; } chmax(d[v], dfs(u) + 1); } calced[v] = 1; return d[v]; } int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) rep(j, n - 1) { int t; cin >> t; t--; a[i][j] = t; } int v; rep(i, n) rep(j, n) { if (i < j) { id[i][j] = v; v++; } } rep(i, n) rep(j, n - 2) { int from = calc_v(i, a[i][j]); int tov = calc_v(i, a[i][j + 1]); to[from].push_back(tov); } int ans = 0; rep(i, v) { chmax(ans, dfs(i)); } if (dag) cout << ans + 1 << endl; else cout << -1 << endl; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; const int INF = 1001001001; const long long LINF = 1001002003004005006ll; 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; } typedef long long ll; typedef pair<int, int> P; const int MAXN = 1005; const int MAXV = MAXN * (MAXN - 1) / 2; int id[MAXN][MAXN]; int calc_v(int a, int b) { if (a > b) swap(a, b); return id[a][b]; } vector<int> to[MAXV]; vector<int> d(MAXV, -1); vector<int> calced(MAXV, -1); bool dag = 1; int dfs(int v) { if (d[v] != -1) return d[v]; d[v] = 0; calced[v] = 0; for (int u : to[v]) { if (calced[u] == 0) { dag = 0; continue; } chmax(d[v], dfs(u) + 1); } calced[v] = 1; return d[v]; } int main() { int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); rep(i, n) rep(j, n - 1) { int t; cin >> t; t--; a[i][j] = t; } int v = 0; rep(i, n) rep(j, n) { if (i < j) { id[i][j] = v; v++; } } rep(i, n) rep(j, n - 2) { int from = calc_v(i, a[i][j]); int tov = calc_v(i, a[i][j + 1]); to[from].push_back(tov); } int ans = 0; rep(i, v) { chmax(ans, dfs(i)); } if (dag) cout << ans + 1 << endl; else cout << -1 << endl; }
replace
74
75
74
75
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep1(i, n) for (int i = 1; i <= int(n); i++) #define repR(i, n) for (int i = int(n) - 1; i >= 0; i--) #define rep1R(i, n) for (int i = int(n); i >= 1; i--) #define all(x) (x).begin(), (x).end() #define show(x) #x " = " << x #define mp make_pair #define quit(x) \ { \ cout << (x) << endl; \ return 0; \ } template <class T> bool chmin(T &x, T const &y) { if (x > y) { x = y; return true; } return false; } template <class T> bool chmax(T &x, T const &y) { if (x < y) { x = y; return true; } return false; } template <class T> typename conditional<true, ostream &, decltype(declval<T>().begin())>::type operator<<(ostream &os, T const &v) { for (auto const &x : v) os << x << " "; return os; } template <class T> typename conditional<true, istream &, decltype(declval<T>().begin())>::type operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> ostream &operator<<(ostream &os, pair<T, S> const &p) { return os << "(" << p.fst << ", " << p.snd << ")"; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> const &p) { return is >> p.fst >> p.snd; } int main() { int n; cin >> n; int N = n * (n - 1) / 2; vector<vector<int>> g(N); vector<int> cnt(N); map<pair<int, int>, int> m; int c = 0; rep(i, n) rep(j, n) if (i < j) m[mp(i, j)] = c++; rep(i, n) rep(j, n) if (i > j) m[mp(i, j)] = m[mp(j, i)]; rep(i, n) { vector<int> a(n - 1); rep(j, n - 1) cin >> a[j], a[j]--; rep(j, n - 2) { int x = m[mp(i, a[j])]; int y = m[mp(i, a[j + 1])]; g[x].push_back(y); cnt[y]++; } } deque<int> q; rep(i, N) if (cnt[i] == 0) q.push_back(i); vector<int> a; while (!q.empty()) { int x = q.front(); q.pop_front(); a.push_back(x); for (int y : g[x]) if (--cnt[y] == 0) q.push_back(y); } if (a.size() != N) quit(-1); vector<int> d(N, 1); rep(i, N) { int x = a[i]; for (int y : g[x]) chmax(d[y], d[x] + 1); } rep(i, N) for (int j : g[i]) if (d[i] >= d[j]) quit(-1); cout << *max_element(all(d)) << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep1(i, n) for (int i = 1; i <= int(n); i++) #define repR(i, n) for (int i = int(n) - 1; i >= 0; i--) #define rep1R(i, n) for (int i = int(n); i >= 1; i--) #define all(x) (x).begin(), (x).end() #define show(x) #x " = " << x #define mp make_pair #define quit(x) \ { \ cout << (x) << endl; \ return 0; \ } template <class T> bool chmin(T &x, T const &y) { if (x > y) { x = y; return true; } return false; } template <class T> bool chmax(T &x, T const &y) { if (x < y) { x = y; return true; } return false; } template <class T> typename conditional<true, ostream &, decltype(declval<T>().begin())>::type operator<<(ostream &os, T const &v) { for (auto const &x : v) os << x << " "; return os; } template <class T> typename conditional<true, istream &, decltype(declval<T>().begin())>::type operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> ostream &operator<<(ostream &os, pair<T, S> const &p) { return os << "(" << p.fst << ", " << p.snd << ")"; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> const &p) { return is >> p.fst >> p.snd; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; int N = n * (n - 1) / 2; vector<vector<int>> g(N); vector<int> cnt(N); map<pair<int, int>, int> m; int c = 0; rep(i, n) rep(j, n) if (i < j) m[mp(i, j)] = c++; rep(i, n) rep(j, n) if (i > j) m[mp(i, j)] = m[mp(j, i)]; rep(i, n) { vector<int> a(n - 1); rep(j, n - 1) cin >> a[j], a[j]--; rep(j, n - 2) { int x = m[mp(i, a[j])]; int y = m[mp(i, a[j + 1])]; g[x].push_back(y); cnt[y]++; } } deque<int> q; rep(i, N) if (cnt[i] == 0) q.push_back(i); vector<int> a; while (!q.empty()) { int x = q.front(); q.pop_front(); a.push_back(x); for (int y : g[x]) if (--cnt[y] == 0) q.push_back(y); } if (a.size() != N) quit(-1); vector<int> d(N, 1); rep(i, N) { int x = a[i]; for (int y : g[x]) chmax(d[y], d[x] + 1); } rep(i, N) for (int j : g[i]) if (d[i] >= d[j]) quit(-1); cout << *max_element(all(d)) << endl; }
insert
53
53
53
56
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<double> vd; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; typedef vector<pdd> vdd; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define mem(a, b) memset(a, b, sizeof(a)) #define all(x) (x).begin(), (x).end() #define INF 1000000000000 #define MOD 1000000007 #define PB push_back #define MP make_pair #define F first #define S second inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInverse(ll a) { return modPow(a, MOD - 2); } inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int vis[300300], dis[300300]; vvi g(300300); int dfs(int v) { if (vis[v] == 2) return dis[v]; vis[v] = 1; for (auto x : g[v]) { if (vis[x] == 1) return -1; int y = dfs(x); if (y == -1) return -1; dis[v] = max(dis[v], y + 1); } vis[v] = 2; return dis[v]; } int main() { int n; cin >> n; mem(dis, 0); mem(vis, 0); rep(i, n) { int last = 0; rep(j, n - 1) { int a; cin >> a; a--; int x = min(i, a), y = max(i, a); if (last == 0) { last = y + 1000 * x; g[0].PB(last); continue; } g[last].PB(y + 1000 * x); last = y + 1000 * x; } } dis[0] = -1; cout << dfs(0) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<double> vd; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; typedef vector<pdd> vdd; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define mem(a, b) memset(a, b, sizeof(a)) #define all(x) (x).begin(), (x).end() #define INF 1000000000000 #define MOD 1000000007 #define PB push_back #define MP make_pair #define F first #define S second inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a * b) % MOD; } inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a + b) % MOD; } inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInverse(ll a) { return modPow(a, MOD - 2); } inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int vis[1000000], dis[1000000]; vvi g(1000000); int dfs(int v) { if (vis[v] == 2) return dis[v]; vis[v] = 1; for (auto x : g[v]) { if (vis[x] == 1) return -1; int y = dfs(x); if (y == -1) return -1; dis[v] = max(dis[v], y + 1); } vis[v] = 2; return dis[v]; } int main() { int n; cin >> n; mem(dis, 0); mem(vis, 0); rep(i, n) { int last = 0; rep(j, n - 1) { int a; cin >> a; a--; int x = min(i, a), y = max(i, a); if (last == 0) { last = y + 1000 * x; g[0].PB(last); continue; } g[last].PB(y + 1000 * x); last = y + 1000 * x; } } dis[0] = -1; cout << dfs(0) << endl; }
replace
64
66
64
66
0
p02925
C++
Time Limit Exceeded
/*************************************************************** File name: E.cpp Author: ljfcnyali Create time: 2019年09月01日 星期日 20时16分00秒 ***************************************************************/ #include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i) #define mem(a) memset((a), 0, sizeof(a)) #define str(a) strlen(a) typedef long long LL; const int maxn = 1010; queue<int> Q[maxn]; int n; bool vis[maxn]; int main() { scanf("%d", &n); REP(i, 1, n) REP(j, 1, n - 1) { int x; scanf("%d", &x); Q[i].push(x); } int i = 1; while (i) { bool flag = true; REP(i, 1, n) if (!Q[i].empty()) { flag = false; break; } if (flag == true) { printf("%d\n", i - 1); return 0; } mem(vis); REP(i, 1, n) { if (vis[i] || vis[Q[i].front()]) continue; if (Q[Q[i].front()].front() == i) { vis[Q[i].front()] = vis[i] = true; Q[Q[i].front()].pop(); Q[i].pop(); flag = true; } } if (!flag) { printf("-1\n"); return 0; } ++i; } return 0; }
/*************************************************************** File name: E.cpp Author: ljfcnyali Create time: 2019年09月01日 星期日 20时16分00秒 ***************************************************************/ #include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i) #define mem(a) memset((a), 0, sizeof(a)) #define str(a) strlen(a) typedef long long LL; const int maxn = 1010; queue<int> Q[maxn]; int n; bool vis[maxn]; int main() { scanf("%d", &n); REP(i, 1, n) REP(j, 1, n - 1) { int x; scanf("%d", &x); Q[i].push(x); } int i = 1; while (i) { bool flag = true; REP(i, 1, n) if (!Q[i].empty()) { flag = false; break; } if (flag == true) { printf("%d\n", i - 1); return 0; } mem(vis); REP(i, 1, n) { if (Q[i].empty() || Q[Q[i].front()].empty()) continue; if (vis[i] || vis[Q[i].front()]) continue; if (Q[Q[i].front()].front() == i) { vis[Q[i].front()] = vis[i] = true; Q[Q[i].front()].pop(); Q[i].pop(); flag = true; } } if (!flag) { printf("-1\n"); return 0; } ++i; } return 0; }
insert
41
41
41
43
TLE
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <queue> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef int ll; ll mix_player_id(ll l, ll r) { if (l > r) { swap(l, r); } return l * 1000 + r; } int main(int argc, char *argv[]) { ll n; cin >> n; vector<queue<ll>> players(n); for (ll i = 0; i < n; i++) { for (ll j = 0; j < n - 1; j++) { ll opp; cin >> opp; opp--; players[i].push(mix_player_id(i, opp)); } } vector<ll> found(2000000, -1); int days = 0; vector<ll> update; vector<ll> next_update; for (ll i = 0; i < players.size(); i++) { update.push_back(i); } while (true) { bool is_finished = true; bool progressed = false; vector<bool> fought(1000); for (ll i : update) { if (fought[i]) continue; auto &q = players[i]; if (q.empty()) continue; auto current_idx = q.front(); is_finished = false; if (found[current_idx] >= 0 && found[current_idx] != i) { players[found[current_idx]].pop(); q.pop(); progressed = true; fought[i] = true; fought[found[current_idx]] = true; next_update.push_back(i); next_update.push_back(found[current_idx]); found.at(current_idx) = -1; } else { found.at(current_idx) = i; } } if (is_finished) { break; } if (!progressed) { cout << -1 << endl; return 0; } days++; swap(update, next_update); } cout << days << endl; }
#include <algorithm> #include <array> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <queue> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef int ll; ll mix_player_id(ll l, ll r) { if (l > r) { swap(l, r); } return l * 1000 + r; } int main(int argc, char *argv[]) { ll n; cin >> n; vector<queue<ll>> players(n); for (ll i = 0; i < n; i++) { for (ll j = 0; j < n - 1; j++) { ll opp; cin >> opp; opp--; players[i].push(mix_player_id(i, opp)); } } vector<ll> found(2000000, -1); int days = 0; vector<ll> update; vector<ll> next_update; for (ll i = 0; i < players.size(); i++) { update.push_back(i); } while (true) { next_update.clear(); bool is_finished = true; bool progressed = false; vector<bool> fought(1000); for (ll i : update) { if (fought[i]) continue; auto &q = players[i]; if (q.empty()) continue; auto current_idx = q.front(); is_finished = false; if (found[current_idx] >= 0 && found[current_idx] != i) { players[found[current_idx]].pop(); q.pop(); progressed = true; fought[i] = true; fought[found[current_idx]] = true; next_update.push_back(i); next_update.push_back(found[current_idx]); found.at(current_idx) = -1; } else { found.at(current_idx) = i; } } if (is_finished) { break; } if (!progressed) { cout << -1 << endl; return 0; } days++; swap(update, next_update); } cout << days << endl; }
insert
44
44
44
45
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka int main() { ios::sync_with_stdio(false); int n; vector<vector<int>> a(n, vector<int>(n - 1)); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; } }; vector<int> c(n); vector<int> q; for (int i = 0; i < n; i++) { int x = a[i][0]; if (a[x][0] == i) { q.push_back(i); } } int res = 0; while (!q.empty()) { res++; vector<int> q2; for (int i : q) { c[i]++; if (c[i] < n - 1) { int x = a[i][c[i]]; if (a[x][c[x]] == i) { q2.push_back(i); q2.push_back(x); } } } q = q2; } for (int i = 0; i < n; i++) { if (c[i] < n - 1) { cout << -1; return 0; } } cout << res; return 0; }
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<vector<int>> a(n, vector<int>(n - 1)); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; a[i][j]--; } }; vector<int> c(n); vector<int> q; for (int i = 0; i < n; i++) { int x = a[i][0]; if (a[x][0] == i) { q.push_back(i); } } int res = 0; while (!q.empty()) { res++; vector<int> q2; for (int i : q) { c[i]++; if (c[i] < n - 1) { int x = a[i][c[i]]; if (a[x][c[x]] == i) { q2.push_back(i); q2.push_back(x); } } } q = q2; } for (int i = 0; i < n; i++) { if (c[i] < n - 1) { cout << -1; return 0; } } cout << res; return 0; }
insert
12
12
12
13
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p02925
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } typedef pair<int, int> P; lint N; const lint MAXN = 1005; const lint MAXV = MAXN * (MAXN - 1) / 2; int dp[MAXN]; vector<lint> to[MAXV]; lint id[MAXN][MAXN]; bool visited[MAXV]; bool calculated[MAXV]; int ToID(int i, int j) { if (i > j) swap(i, j); return id[i][j]; } int dfs(int v) { if (visited[v]) { if (!calculated[v]) return -1; return dp[v]; } visited[v] = true; dp[v] = 1; for (auto u : to[v]) { int res = dfs(u); if (res == -1) return -1; dp[v] = max(dp[v], res + 1); } calculated[v] = true; return dp[v]; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; vector<vector<int>> vec(N, vector<int>(N - 1)); REP(i, N) { REP(j, N - 1) { cin >> vec[i][j]; vec[i][j]--; } } int V = 0; REP(i, N) { REP(j, N) { if (i < j) { id[i][j] = V++; } } } REP(i, N) { REP(j, N - 1) { vec[i][j] = ToID(i, vec[i][j]); } REP(j, N - 2) { to[vec[i][j + 1]].push_back(vec[i][j]); } } int ans = 0; REP(i, V) { int res = dfs(i); if (res == -1) { cout << -1; return 0; } ans = max(ans, dfs(i)); } cout << ans; }
#include "bits/stdc++.h" using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } typedef pair<int, int> P; lint N; const lint MAXN = 1005; const lint MAXV = MAXN * (MAXN - 1) / 2; int dp[MAXV]; vector<lint> to[MAXV]; lint id[MAXN][MAXN]; bool visited[MAXV]; bool calculated[MAXV]; int ToID(int i, int j) { if (i > j) swap(i, j); return id[i][j]; } int dfs(int v) { if (visited[v]) { if (!calculated[v]) return -1; return dp[v]; } visited[v] = true; dp[v] = 1; for (auto u : to[v]) { int res = dfs(u); if (res == -1) return -1; dp[v] = max(dp[v], res + 1); } calculated[v] = true; return dp[v]; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; vector<vector<int>> vec(N, vector<int>(N - 1)); REP(i, N) { REP(j, N - 1) { cin >> vec[i][j]; vec[i][j]--; } } int V = 0; REP(i, N) { REP(j, N) { if (i < j) { id[i][j] = V++; } } } REP(i, N) { REP(j, N - 1) { vec[i][j] = ToID(i, vec[i][j]); } REP(j, N - 2) { to[vec[i][j + 1]].push_back(vec[i][j]); } } int ans = 0; REP(i, V) { int res = dfs(i); if (res == -1) { cout << -1; return 0; } ans = max(ans, dfs(i)); } cout << ans; }
replace
27
28
27
28
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n; int a[N][N]; int idx[N]; bool waiting[N][N]; int main() { scanf("%d", &n); vector<int> toCheck; for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { scanf("%d", &a[i][j]); --a[i][j]; } idx[i] = 0; toCheck.push_back(i); } int day = 0; bool can = true; while (can) { can = false; vector<int> ncheck; for (int u : toCheck) { int v = a[u][idx[u]]; if (v < u) swap(u, v); if (waiting[u][v]) { can = true; waiting[u][v] = false; ++idx[u], ++idx[v]; if (idx[u] < n - 1) ncheck.push_back(u); if (idx[v] < n - 1) ncheck.push_back(v); } else { waiting[u][v] = true; } } ncheck = toCheck; ++day; } for (int i = 0; i < n; ++i) { if (idx[i] != n - 1) day = -1; } printf("%d\n", day); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n; int a[N][N]; int idx[N]; bool waiting[N][N]; int main() { scanf("%d", &n); vector<int> toCheck; for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { scanf("%d", &a[i][j]); --a[i][j]; } idx[i] = 0; toCheck.push_back(i); } int day = 0; bool can = true; while (can) { can = false; vector<int> ncheck; for (int u : toCheck) { int v = a[u][idx[u]]; if (v < u) swap(u, v); if (waiting[u][v]) { can = true; waiting[u][v] = false; ++idx[u], ++idx[v]; if (idx[u] < n - 1) ncheck.push_back(u); if (idx[v] < n - 1) ncheck.push_back(v); } else { waiting[u][v] = true; } } toCheck = ncheck; if (can) ++day; } for (int i = 0; i < n; ++i) { if (idx[i] != n - 1) day = -1; } printf("%d\n", day); return 0; }
replace
49
51
49
52
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++) #define fi first #define se second long long mo = 1000000007; typedef long long ll; typedef long double ld; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } template <class A> void PR(A a, ll n) { rep(i, n) { if (i) cout << ' '; cout << a[i]; } cout << "\n"; } ld PI = 3.14159265358979323846; int main() { ll N; cin >> N; vector<vector<ll>> A(N, vector<ll>(N - 1)); rep(i, N) { rep(j, N - 1) { cin >> A[i][j]; A[i][j]--; } } vector<queue<ll>> que(N); rep(i, N) { rep(j, N - 1) { que[i].push(A[i][j]); } } ll ans = 0; ll cnt = 0; rep(j, N * (N - 1) / 2) { ans++; map<ll, ll> mp; bool flag = false; rep(i, N) { if (mp[i] > 0) continue; if (que[i].empty()) continue; ll t = que[i].front(); if (mp[t] > 0) continue; if (que[t].front() == i) { flag = true; que[i].pop(); que[t].pop(); mp[i]++; mp[t]++; cnt++; } else { continue; } } // cout << ans << " " << cnt << endl; if (!flag) { bool flag2 = false; rep(i, N) { if (!que[i].empty()) flag2 = true; } if (flag2) { cout << -1 << endl; return 0; } else { ans--; break; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++) #define fi first #define se second long long mo = 1000000007; typedef long long ll; typedef long double ld; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } template <class A> void PR(A a, ll n) { rep(i, n) { if (i) cout << ' '; cout << a[i]; } cout << "\n"; } ld PI = 3.14159265358979323846; int main() { ll N; cin >> N; vector<vector<ll>> A(N, vector<ll>(N - 1)); rep(i, N) { rep(j, N - 1) { cin >> A[i][j]; A[i][j]--; } } vector<queue<ll>> que(N); rep(i, N) { rep(j, N - 1) { que[i].push(A[i][j]); } } ll ans = 0; ll cnt = 0; rep(j, N * (N - 1) / 2) { ans++; vector<ll> mp(N, 0); bool flag = false; rep(i, N) { if (mp[i] > 0) continue; if (que[i].empty()) continue; ll t = que[i].front(); if (mp[t] > 0) continue; if (que[t].front() == i) { flag = true; que[i].pop(); que[t].pop(); mp[i]++; mp[t]++; cnt++; } else { continue; } } // cout << ans << " " << cnt << endl; if (!flag) { bool flag2 = false; rep(i, N) { if (!que[i].empty()) flag2 = true; } if (flag2) { cout << -1 << endl; return 0; } else { ans--; break; } } } cout << ans << endl; }
replace
47
48
47
48
TLE
p02925
C++
Time Limit Exceeded
/* author : mpily :-() title : Competing.cpp time :Mon Sep 2 10:24:03 2019 */ #include <bits/stdc++.h> using namespace std; int main() { int n; int daycount = 0; cin >> n; vector<vector<int>> arr(n + 1); vector<int> tops(n + 1); for (int i = 1; i <= n; i++) { for (int j = 0; j < n - 1; j++) { int temp; cin >> temp; arr[i].push_back(temp); } tops[i] = 0; } int gamesCount = 0; while (gamesCount < (n * (n - 1)) / 2) { bool haschosen = 0; vector<int> toPlay; for (int i = 1; i <= n; i++) { if (tops[i] == n - 1) continue; int expected = arr[i][tops[i]]; if (arr[expected][tops[expected]] == i && i < expected) { gamesCount += 1; // cout<<gamesCount<<" : "<<i<<expected<<"\n"; toPlay.push_back(i); toPlay.push_back(expected); haschosen = 1; } } if (haschosen) { // cout<<"here"; for (auto &a : toPlay) { tops[a] += 1; } daycount += 1; } else { cout << -1 << "\n"; return 0; } } cout << daycount << "\n"; return 0; }
/* author : mpily :-() title : Competing.cpp time :Mon Sep 2 10:24:03 2019 */ #include <bits/stdc++.h> using namespace std; int main() { int n; int daycount = 0; cin >> n; vector<vector<int>> arr(n + 1); vector<int> tops(n + 1); for (int i = 1; i <= n; i++) { for (int j = 0; j < n - 1; j++) { int temp; cin >> temp; arr[i].push_back(temp); } tops[i] = 0; } int gamesCount = 0; while (gamesCount < (n * (n - 1)) / 2) { bool haschosen = 0; vector<int> toPlay; for (int i = 1; i <= n; i++) { if (tops[i] == n - 1) continue; int expected = arr[i][tops[i]]; if (arr[expected][tops[expected]] == i && i < expected) { gamesCount += 1; // cout<<gamesCount<<" : "<<i<<expected<<"\n"; toPlay.push_back(i); toPlay.push_back(expected); haschosen = 1; } } if (haschosen) { // cout<<"here"; for (auto &a : toPlay) { tops[a] += 1; } daycount += 1; } else { cout << -1 << "\n"; return 0; } if (daycount >= 100000) { cout << (n * (n - 1)) / 2 << "\n"; return 0; } } cout << daycount << "\n"; return 0; }
insert
47
47
47
51
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define erep(i, a, n) for (int i = a; i <= n; i++) typedef long long ll; #define int long long #define vint vector<int> #define vvint vector<vector<int>> #define vstring vector<string> #define vdouble vector<double> #define vll vector<ll>: #define vbool vector<bool> #define INF 1101010101010101010 #define MOD 1000000007 #define P pair<int, int> 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; } const int MAXN = 1010; const int MAXV = MAXN * (MAXN - 1) / 2; vint to[MAXV]; int id[MAXN][MAXN]; bool visited[MAXV]; int dp[MAXV]; bool calculated[MAXV]; int dfs(int a) { if (visited[a]) { if (!calculated[a]) { return -1; } return dp[a]; } visited[a] = true; dp[a] = 1; for (int p : to[a]) { int u = dfs(p); if (u == -1) { return -1; } dp[a] = max(dp[a], u + 1); } calculated[a] = true; return dp[a]; } int toID(int a, int b) { if (a < b) swap(a, b); return id[a][b]; } signed main() { int n; cin >> n; vvint A(n, vint(n - 1)); rep(i, 0, n) { rep(j, 0, n - 1) { cin >> A[i][j]; A[i][j]--; } } int v = 0; rep(i, 0, n) { rep(j, 0, n - 1) { id[i][j] = v; v++; } } rep(i, 0, n) { rep(j, 0, n - 1) { A[i][j] = toID(i, A[i][j]); } } rep(i, 0, n) { rep(j, 0, n - 2) { to[A[i][j + 1]].push_back(A[i][j]); } } int ans = 0; rep(i, 0, v) { int res = dfs(i); if (res == -1) { cout << -1 << endl; return 0; } ans = max(ans, res); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define erep(i, a, n) for (int i = a; i <= n; i++) typedef long long ll; #define int long long #define vint vector<int> #define vvint vector<vector<int>> #define vstring vector<string> #define vdouble vector<double> #define vll vector<ll>: #define vbool vector<bool> #define INF 1101010101010101010 #define MOD 1000000007 #define P pair<int, int> 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; } const int MAXN = 1010; const int MAXV = MAXN * (MAXN - 1) / 2; vint to[MAXV]; int id[MAXN][MAXN]; bool visited[MAXV]; int dp[MAXV]; bool calculated[MAXV]; int dfs(int a) { if (visited[a]) { if (!calculated[a]) { return -1; } return dp[a]; } visited[a] = true; dp[a] = 1; for (int p : to[a]) { int u = dfs(p); if (u == -1) { return -1; } dp[a] = max(dp[a], u + 1); } calculated[a] = true; return dp[a]; } int toID(int a, int b) { if (a < b) swap(a, b); return id[a][b]; } signed main() { int n; cin >> n; vvint A(n, vint(n - 1)); rep(i, 0, n) { rep(j, 0, n - 1) { cin >> A[i][j]; A[i][j]--; } } int v = 0; rep(i, 0, n) { rep(j, 0, n) { if (i > j) { id[i][j] = v; v++; } } } rep(i, 0, n) { rep(j, 0, n - 1) { A[i][j] = toID(i, A[i][j]); } } rep(i, 0, n) { rep(j, 0, n - 2) { to[A[i][j + 1]].push_back(A[i][j]); } } int ans = 0; rep(i, 0, v) { int res = dfs(i); if (res == -1) { cout << -1 << endl; return 0; } ans = max(ans, res); } cout << ans << endl; }
replace
79
82
79
84
0
p02925
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPr(i, n) for (int i = (n)-1; i >= 0; --i) #define FORq(i, m, n) for (int i = (m); i <= (n); ++i) #define FORqr(i, m, n) for (int i = (n); i >= (m); --i) #define PB push_back #define MP make_pair #define DEBUG printf("%s\n", "debug") #define fst first #define snd second #define SIN(x, S) (S.count(x) != 0) #define M0(x) memset(x, 0, sizeof(x)) #define FILL(x, y) memset(x, y, sizeof(x)) #define MM(x) memset(x, -1, sizeof(x)) #define ALL(x) (x).begin(), (x).end() #define SCD(n) scanf("%d", &n) #define SCD2(m, n) scanf("%d%d", &m, &n) #define SCD3(m, n, k) scanf("%d%d%d", &m, &n, &k) #define SCLLD(n) scanf("%lld", &n) #define SCLLD2(m, n) scanf("%lld%lld", &m, &n) #define SCLLD3(m, n, k) scanf("%lld%lld%lld", &m, &n, &k) using namespace std; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef long long ll; typedef long long integer; /////////////////////////////////////////////// const ll MOD = 1e9 + 7; ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } 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 <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept { return is >> x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; /////////////////////////////////////////////// /*(._.)*/ int INF = 1e9; static vector<PII> E[1022][1022]; int indeg[1022][1022] = {}; int len[1022][1022] = {}; bool visited[1022][1022] = {}; bool cycle = false; int mcnt = 10000000; int cnt = 0; int dfs(int i, int j) { cnt++; if (mcnt < cnt) { cycle = true; return 0; } visited[i][j] = true; if (len[i][j] != 0) { // visited[i][j] = false; return len[i][j]; } int siz = E[i][j].size(); if (siz == 0) { visited[i][j] = false; return 0; } else { int ret = 0; REP(k, siz) { if (mcnt < cnt) { cycle = true; return 0; } if (visited[E[i][j][k].fst][E[i][j][k].snd]) cycle = true; ret = max(ret, 1 + dfs(E[i][j][k].fst, E[i][j][k].snd)); } visited[i][j] = false; return len[i][j] = ret; } } int main() { int N; cin >> N; vector<vector<int>> A(N + 200); REP(i, N) { REP(j, N - 1) { int a; cin >> a; A[i + 1].PB(a); } } FORq(i, 1, N) { REP(j, N - 2) { int nowi = min(i, A[i][j]); int nowj = max(i, A[i][j]); int nexti = min(i, A[i][j + 1]); int nextj = max(i, A[i][j + 1]); E[nowi][nowj].PB(MP(nexti, nextj)); // indeg[nexti][nextj]++; } } for (int i = 1; i <= N; i++) { for (int j = i + 1; j <= N; j++) { sort(E[i][j].begin(), E[i][j].end()); E[i][j].erase(unique(E[i][j].begin(), E[i][j].end()), E[i][j].end()); for (auto p : E[i][j]) { indeg[p.first][p.second]++; } } } // initilize FORq(i, 1, N) { FORq(j, 1, N) { if (i >= j) continue; if (indeg[i][j] == 0) { E[0][0].PB(MP(i, j)); } } } int ans = dfs(0, 0); if ((cycle) or (ans == 0)) ans = -1; cout << ans << endl; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPr(i, n) for (int i = (n)-1; i >= 0; --i) #define FORq(i, m, n) for (int i = (m); i <= (n); ++i) #define FORqr(i, m, n) for (int i = (n); i >= (m); --i) #define PB push_back #define MP make_pair #define DEBUG printf("%s\n", "debug") #define fst first #define snd second #define SIN(x, S) (S.count(x) != 0) #define M0(x) memset(x, 0, sizeof(x)) #define FILL(x, y) memset(x, y, sizeof(x)) #define MM(x) memset(x, -1, sizeof(x)) #define ALL(x) (x).begin(), (x).end() #define SCD(n) scanf("%d", &n) #define SCD2(m, n) scanf("%d%d", &m, &n) #define SCD3(m, n, k) scanf("%d%d%d", &m, &n, &k) #define SCLLD(n) scanf("%lld", &n) #define SCLLD2(m, n) scanf("%lld%lld", &m, &n) #define SCLLD3(m, n, k) scanf("%lld%lld%lld", &m, &n, &k) using namespace std; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef long long ll; typedef long long integer; /////////////////////////////////////////////// const ll MOD = 1e9 + 7; ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } 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 <int MOD> struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept { return os << x.val; } friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept { return is >> x.val; } friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; /////////////////////////////////////////////// /*(._.)*/ int INF = 1e9; static vector<PII> E[1022][1022]; int indeg[1022][1022] = {}; int len[1022][1022] = {}; bool visited[1022][1022] = {}; bool cycle = false; int mcnt = 10000000; int cnt = 0; int dfs(int i, int j) { cnt++; if (mcnt < cnt) { cycle = true; return 0; } if (cycle) return 0; visited[i][j] = true; if (len[i][j] != 0) { // visited[i][j] = false; return len[i][j]; } int siz = E[i][j].size(); if (siz == 0) { visited[i][j] = false; return 0; } else { int ret = 0; REP(k, siz) { if (mcnt < cnt) { cycle = true; return 0; } if (visited[E[i][j][k].fst][E[i][j][k].snd]) cycle = true; ret = max(ret, 1 + dfs(E[i][j][k].fst, E[i][j][k].snd)); } visited[i][j] = false; return len[i][j] = ret; } } int main() { int N; cin >> N; vector<vector<int>> A(N + 200); REP(i, N) { REP(j, N - 1) { int a; cin >> a; A[i + 1].PB(a); } } FORq(i, 1, N) { REP(j, N - 2) { int nowi = min(i, A[i][j]); int nowj = max(i, A[i][j]); int nexti = min(i, A[i][j + 1]); int nextj = max(i, A[i][j + 1]); E[nowi][nowj].PB(MP(nexti, nextj)); // indeg[nexti][nextj]++; } } for (int i = 1; i <= N; i++) { for (int j = i + 1; j <= N; j++) { sort(E[i][j].begin(), E[i][j].end()); E[i][j].erase(unique(E[i][j].begin(), E[i][j].end()), E[i][j].end()); for (auto p : E[i][j]) { indeg[p.first][p.second]++; } } } // initilize FORq(i, 1, N) { FORq(j, 1, N) { if (i >= j) continue; if (indeg[i][j] == 0) { E[0][0].PB(MP(i, j)); } } } int ans = dfs(0, 0); if ((cycle) or (ans == 0)) ans = -1; cout << ans << endl; }
replace
129
130
129
131
0
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; void dfs(vector<vector<int>> &B, int tmp, int len, vector<bool> &vis, vector<bool> &vis2, bool &flag, vector<int> &depth) { if (flag) return; if (B[tmp].empty()) { depth[tmp] = max(depth[tmp], 1); } else { for (auto ne : B[tmp]) { if (vis[ne]) { if (vis2[ne]) flag = true; depth[tmp] = max(depth[tmp], depth[ne] + 1); } else { vis[ne] = true; vis2[ne] = true; dfs(B, ne, len + 1, vis, vis2, flag, depth); depth[tmp] = max(depth[tmp], depth[ne] + 1); vis2[ne] = false; } } } } int main() { int N; cin >> N; vector<vector<int>> mp(N, vector<int>(N)); { int cnt = 0; rep(i, N) { REP(j, i + 1, N) { mp[i][j] = cnt; mp[j][i] = cnt; cnt++; } } } vector<vector<int>> A(N, vector<int>(N - 1)); int M = N * (N - 1) / 2; vector<vector<int>> B(M), C(M); rep(i, N) { int prev; rep(j, N - 1) { cin >> A[i][j]; A[i][j]--; int tmp = mp[i][A[i][j]]; if (j > 0) { B[prev].push_back(tmp); C[tmp].push_back(prev); } prev = tmp; } } bool flag = false; vector<bool> vis(M); vector<int> depth(M); rep(i, C.size()) { vis[i] = true; vector<bool> vis2(M); vis2[i] = true; dfs(B, i, 1, vis, vis2, flag, depth); } int ans = -1; rep(i, M) ans = max(ans, depth[i]); if (flag) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; void dfs(vector<vector<int>> &B, int tmp, int len, vector<bool> &vis, vector<bool> &vis2, bool &flag, vector<int> &depth) { if (flag) return; if (B[tmp].empty()) { depth[tmp] = max(depth[tmp], 1); } else { for (auto ne : B[tmp]) { if (vis[ne]) { if (vis2[ne]) flag = true; depth[tmp] = max(depth[tmp], depth[ne] + 1); } else { vis[ne] = true; vis2[ne] = true; dfs(B, ne, len + 1, vis, vis2, flag, depth); depth[tmp] = max(depth[tmp], depth[ne] + 1); vis2[ne] = false; } } } } int main() { int N; cin >> N; vector<vector<int>> mp(N, vector<int>(N)); { int cnt = 0; rep(i, N) { REP(j, i + 1, N) { mp[i][j] = cnt; mp[j][i] = cnt; cnt++; } } } vector<vector<int>> A(N, vector<int>(N - 1)); int M = N * (N - 1) / 2; vector<vector<int>> B(M), C(M); rep(i, N) { int prev; rep(j, N - 1) { cin >> A[i][j]; A[i][j]--; int tmp = mp[i][A[i][j]]; if (j > 0) { B[prev].push_back(tmp); C[tmp].push_back(prev); } prev = tmp; } } bool flag = false; vector<bool> vis(M); vector<int> depth(M); rep(i, C.size()) { if (!vis[i]) { vis[i] = true; vector<bool> vis2(M); vis2[i] = true; dfs(B, i, 1, vis, vis2, flag, depth); } } int ans = -1; rep(i, M) ans = max(ans, depth[i]); if (flag) cout << -1 << endl; else cout << ans << endl; return 0; }
replace
66
70
66
72
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define mod (int)(1e9 + 7) #define inf (int)(3e18) #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define P pair<int, int> #define PiP pair<int, pair<int, int>> #define PP pair<P, P> #define all(v) v.begin(), v.end() #define mkp make_pair #define mkt make_tuple #define prique(T) priority_queue<T, vector<T>, greater<T>> #define vecunique(vec) \ sort(vec.begin(), vec.end()); \ decltype(vec)::iterator result = std::unique(vec.begin(), vec.end()); \ vec.erase(result, vec.end()) using namespace std; template <class T> inline void chmax(T &a, T b) { a = max(a, b); } template <class T> inline void chmin(T &a, T b) { a = min(a, b); } bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return x != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int mod_pow(int x, int y, int m) { int res = 1; while (y > 0) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } int get_rand(int MIN, int MAX) { random_device rnd; mt19937_64 mt64(rnd()); uniform_int_distribution<int> engine(MIN, MAX); return engine(mt64); } /*--------Library Zone!--------*/ int n; int a[1010][1010]; vector<int> v[100010]; int g[100010]; int cost[100010]; queue<int> que; signed main() { cin >> n; rep(i, n) { rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; } } rep(i, n) { rep(j, n - 2) { int k = a[i][j], l = i; int k2 = a[i][j + 1], l2 = i; if (l > k) swap(l, k); if (l2 > k2) swap(l2, k2); v[l * n + k].push_back(l2 * n + k2); g[l2 * n + k2]++; } } rep(i, n) { for (int j = i + 1; j < n; j++) { if (!g[i * n + j]) { que.push(i * n + j); cost[i * n + j] = 1; } } } while (!que.empty()) { int p = que.front(); que.pop(); for (auto q : v[p]) { g[q]--; if (!g[q]) { que.push(q); cost[q] = cost[p] + 1; } } } int ma = 0; rep(i, n) { for (int j = i + 1; j < n; j++) { if (!cost[i * n + j]) { cout << -1 << endl; return 0; } chmax(ma, cost[i * n + j]); } } cout << ma << endl; }
#include <bits/stdc++.h> #define int long long #define mod (int)(1e9 + 7) #define inf (int)(3e18) #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define P pair<int, int> #define PiP pair<int, pair<int, int>> #define PP pair<P, P> #define all(v) v.begin(), v.end() #define mkp make_pair #define mkt make_tuple #define prique(T) priority_queue<T, vector<T>, greater<T>> #define vecunique(vec) \ sort(vec.begin(), vec.end()); \ decltype(vec)::iterator result = std::unique(vec.begin(), vec.end()); \ vec.erase(result, vec.end()) using namespace std; template <class T> inline void chmax(T &a, T b) { a = max(a, b); } template <class T> inline void chmin(T &a, T b) { a = min(a, b); } bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return x != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int mod_pow(int x, int y, int m) { int res = 1; while (y > 0) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } int get_rand(int MIN, int MAX) { random_device rnd; mt19937_64 mt64(rnd()); uniform_int_distribution<int> engine(MIN, MAX); return engine(mt64); } /*--------Library Zone!--------*/ int n; int a[1010][1010]; vector<int> v[1000100]; int g[1000010]; int cost[1000010]; queue<int> que; signed main() { cin >> n; rep(i, n) { rep(j, n - 1) { cin >> a[i][j]; a[i][j]--; } } rep(i, n) { rep(j, n - 2) { int k = a[i][j], l = i; int k2 = a[i][j + 1], l2 = i; if (l > k) swap(l, k); if (l2 > k2) swap(l2, k2); v[l * n + k].push_back(l2 * n + k2); g[l2 * n + k2]++; } } rep(i, n) { for (int j = i + 1; j < n; j++) { if (!g[i * n + j]) { que.push(i * n + j); cost[i * n + j] = 1; } } } while (!que.empty()) { int p = que.front(); que.pop(); for (auto q : v[p]) { g[q]--; if (!g[q]) { que.push(q); cost[q] = cost[p] + 1; } } } int ma = 0; rep(i, n) { for (int j = i + 1; j < n; j++) { if (!cost[i * n + j]) { cout << -1 << endl; return 0; } chmax(ma, cost[i * n + j]); } } cout << ma << endl; }
replace
68
71
68
71
0
p02925
C++
Time Limit Exceeded
#include <cassert> // PXjUXeZStdX #include <ccomplex> #include <cctype> #include <cerrno> #include <cfenv> #include <cfloat> #include <cinttypes> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstdbool> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctgmath> #include <ctime> #include <cwchar> #include <cwctype> // C++ #include <algorithm> #include <array> #include <atomic> #include <bitset> #include <chrono> #include <complex> #include <condition_variable> #include <deque> #include <exception> #include <forward_list> #include <fstream> #include <functional> #include <future> #include <initializer_list> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <mutex> #include <new> #include <numeric> #include <ostream> #include <queue> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> // #include <boost/foreach.hpp> // #include <boost/range/algorithm.hpp> #define rep(i, j, k) for (int i = (int)j; i < (int)k; i++) #define ll long long #define Sort(v) sort(all(v)) // #define INF 1e9 #define LINF (1LL << 40) #define END return 0 #define pb push_back #define se second #define fi first #define pb push_back #define all(v) (v).begin(), (v).end() #define MP make_pair #define int long long using namespace std; int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // int dx[]={0,1,0,-1}; // int dy[]={1,0,-1,0}; typedef pair<int, int> P; const long long MOD = 1000000007LL; bool isupper(char c) { if ('A' <= c && c <= 'Z') return 1; return 0; } bool islower(char c) { if ('a' <= c && c <= 'z') return 1; return 0; } bool iskaibun(string s) { for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - i - 1]) return 0; return 1; } bool isnumber(char c) { return ('0' <= c && c <= '9'); } bool isalpha(char c) { return (isupper(c) || islower(c)); } template <typename T> void print(vector<T> v) { for (int i = 0; i < v.size(); i++) { if (i) cout << " "; cout << v[i]; } cout << endl; } int INF = 1e18; template <typename T> void printendl(vector<T> v) { for (auto date : v) cout << date << endl; } template <typename T> void printvv(vector<vector<T>> v) { for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v[i].size(); j++) { if (j) cout << " "; cout << v[i][j]; } cout << endl; } } int gcd(int a, int b) { if (a < b) swap(a, b); if (b == 0) return a; else return gcd(b, a % b); } int powl(int n, int p) { int ret = 1; for (int i = 0; i < p; i++) ret *= n; return ret; } struct Edge { int to, cost; }; struct S { pair<int, int> p; int x; }; // int h,w; // vector<vector<int>> v() signed main() { int n; cin >> n; vector<vector<int>> v(n, vector<int>(n - 1)); rep(i, 0, n) { rep(j, 0, n - 1) { scanf("%lld", &v[i][j]); } } int ans = 0; bool ok = true; int loop; vector<int> idx(n, 0); for (loop = 0;; loop++) { bool finish = true; vector<int> used(n, 0); rep(i, 0, n) { if (idx[i] == n - 1 || idx[v[i][idx[i]] - 1] == n - 1 || used[i] == 1 || used[v[i][idx[i]] - 1] == 1) continue; if (v[v[i][idx[i]] - 1][idx[v[i][idx[i]] - 1]] - 1 == i) { finish = false; used[i] = 1; used[v[i][idx[i]] - 1] = 1; idx[v[i][idx[i]] - 1]++; idx[i]++; } } if (loop > 1000000) { cout << 499500 << endl; return 0; } if (finish) break; } rep(i, 0, n) { if (idx[i] < n - 1) { cout << -1 << endl; return 0; } } cout << loop << endl; } /* */
#include <cassert> // PXjUXeZStdX #include <ccomplex> #include <cctype> #include <cerrno> #include <cfenv> #include <cfloat> #include <cinttypes> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstdbool> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctgmath> #include <ctime> #include <cwchar> #include <cwctype> // C++ #include <algorithm> #include <array> #include <atomic> #include <bitset> #include <chrono> #include <complex> #include <condition_variable> #include <deque> #include <exception> #include <forward_list> #include <fstream> #include <functional> #include <future> #include <initializer_list> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <mutex> #include <new> #include <numeric> #include <ostream> #include <queue> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <system_error> #include <thread> #include <tuple> #include <type_traits> #include <typeindex> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> // #include <boost/foreach.hpp> // #include <boost/range/algorithm.hpp> #define rep(i, j, k) for (int i = (int)j; i < (int)k; i++) #define ll long long #define Sort(v) sort(all(v)) // #define INF 1e9 #define LINF (1LL << 40) #define END return 0 #define pb push_back #define se second #define fi first #define pb push_back #define all(v) (v).begin(), (v).end() #define MP make_pair #define int long long using namespace std; int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // int dx[]={0,1,0,-1}; // int dy[]={1,0,-1,0}; typedef pair<int, int> P; const long long MOD = 1000000007LL; bool isupper(char c) { if ('A' <= c && c <= 'Z') return 1; return 0; } bool islower(char c) { if ('a' <= c && c <= 'z') return 1; return 0; } bool iskaibun(string s) { for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - i - 1]) return 0; return 1; } bool isnumber(char c) { return ('0' <= c && c <= '9'); } bool isalpha(char c) { return (isupper(c) || islower(c)); } template <typename T> void print(vector<T> v) { for (int i = 0; i < v.size(); i++) { if (i) cout << " "; cout << v[i]; } cout << endl; } int INF = 1e18; template <typename T> void printendl(vector<T> v) { for (auto date : v) cout << date << endl; } template <typename T> void printvv(vector<vector<T>> v) { for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v[i].size(); j++) { if (j) cout << " "; cout << v[i][j]; } cout << endl; } } int gcd(int a, int b) { if (a < b) swap(a, b); if (b == 0) return a; else return gcd(b, a % b); } int powl(int n, int p) { int ret = 1; for (int i = 0; i < p; i++) ret *= n; return ret; } struct Edge { int to, cost; }; struct S { pair<int, int> p; int x; }; // int h,w; // vector<vector<int>> v() signed main() { int n; cin >> n; vector<vector<int>> v(n, vector<int>(n - 1)); rep(i, 0, n) { rep(j, 0, n - 1) { scanf("%lld", &v[i][j]); } } int ans = 0; bool ok = true; int loop; vector<int> idx(n, 0); for (loop = 0;; loop++) { bool finish = true; vector<int> used(n, 0); rep(i, 0, n) { if (idx[i] == n - 1 || idx[v[i][idx[i]] - 1] == n - 1 || used[i] == 1 || used[v[i][idx[i]] - 1] == 1) continue; if (v[v[i][idx[i]] - 1][idx[v[i][idx[i]] - 1]] - 1 == i) { finish = false; used[i] = 1; used[v[i][idx[i]] - 1] = 1; idx[v[i][idx[i]] - 1]++; idx[i]++; } } if (loop > 100000) { cout << 499500 << endl; return 0; } if (finish) break; } rep(i, 0, n) { if (idx[i] < n - 1) { cout << -1 << endl; return 0; } } cout << loop << endl; } /* */
replace
197
198
197
198
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n; queue<int> q[1001]; bool vis[1001]; int f() { memset(vis, 0, sizeof vis); bool flag = 0; for (int i = 1; i <= n; ++i) { if (vis[i]) continue; if (q[i].empty()) continue; int x = q[i].front(); if (q[x].empty()) continue; if (q[x].front() == i and !vis[i] and !vis[x]) { flag = 1; vis[x] = vis[i] = 1; q[x].pop(); q[i].pop(); } } if (!flag) return -1; for (int i = 1; i <= n; ++i) if (q[i].size()) return 1; return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { for (int j = 0; j < n - 1; ++j) { int x; scanf("%d", &x); q[i].push(x); } } int ans = 0; int ini = 1; int lim = 1e7; while (ini == 1 and lim--) ini = f(), ans++; if (ini == -1) puts("-1"); else printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n; queue<int> q[1001]; bool vis[1001]; int f() { memset(vis, 0, sizeof vis); bool flag = 0; for (int i = 1; i <= n; ++i) { if (vis[i]) continue; if (q[i].empty()) continue; int x = q[i].front(); if (q[x].empty()) continue; if (q[x].front() == i and !vis[i] and !vis[x]) { flag = 1; vis[x] = vis[i] = 1; q[x].pop(); q[i].pop(); } } if (!flag) return -1; for (int i = 1; i <= n; ++i) if (q[i].size()) return 1; return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { for (int j = 0; j < n - 1; ++j) { int x; scanf("%d", &x); q[i].push(x); } } int ans = 0; int ini = 1; int lim = 1000010; while (ini == 1 and lim--) ini = f(), ans++; if (ini == -1) puts("-1"); else printf("%d\n", ans); return 0; }
replace
47
48
47
48
TLE
p02925
C++
Time Limit Exceeded
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // clang-format off #define pb push_back #define eb emplace_back #define fi first #define se second #define each(x,v) for(auto& x : v) #define all(v) (v).begin(),(v).end() #define sz(v) ((int)(v).size()) #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) #define in2(s,t) rep(i,sz(s)){in(s[i] , t[i]);} #define in3(s,t,u) rep(i,sz(s)){in(s[i] , t[i] , u[i]);} #define in4(s,t,u,v) rep(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);} #ifdef ONLINE_JUDGE #define rep(i,N) for(int i = 0; i < (int)(N); i++) #define repr(i,N) for(int i = (int)(N) - 1; i >= 0; i--) #define rep1(i,N) for(int i = 1; i <= (int)(N) ; i++) #define repr1(i,N) for(int i = (N) ; (int)(i) > 0 ; i--) #else #define rep(i,N) for(long long i = 0; i < (long long)(N); i++) #define repr(i,N) for(long long i = (long long)(N) - 1; i >= 0; i--) #define rep1(i,N) for(long long i = 1; i <= (long long)(N) ; i++) #define repr1(i,N) for(long long i = (N) ; (long long)(i) > 0 ; i--) #endif using namespace std; void solve(); using ll = long long; template<class T = ll> using V = vector<T>; using vi = V<int>; using vl = V<>; using vvi = V< V<int> >; using vd = V<double>; using vs = V<string>; using vvl = V< V<> >; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template<typename T,typename U>ll ceil(T a,U b){return (a + b - 1) / b;} template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); for(int i=0;i<s;i++) os << (i ? " " : "") << v[i]; return os; } template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; } void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);} template<typename T>void die(T x){out(x); exit(0);} #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0) #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);} while(0) #define trcc(v) do { cerr << "name : " << #v << "\n"; int cnt = 0; each(x , v){cerr << (cnt++) << " : "; trc(x); } } while(0) #else #define trc(...) #define trca(...) #define trcc(...) int main(){solve();} #endif constexpr ll TEN(int n){ll ret=1,x=10;while(n){if(n&1)ret*=x;x*=x;n>>=1;}return ret;} struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya; using P = pair<ll,ll>; using vp = V<P>; constexpr int MOD = /**/ 1000000007; //*/ 998244353; // clang-format on ///////////// void solve() { ini(N); vvi g(N, vi(N - 1)); in(g); each(v, g) { each(x, v) x--; v.pb(inf); } // 愚直 vi idx(N, 0); ll ans = 0; auto start = clock(); while (1) { // if(1.0*(clock()-start)*CLOCKS_PER_SEC > 1.7) die((N-1)*(N-2)/2); int upd = 0; vi dat(N, 0); rep(i, N) rep(j, i) { if (g[i][idx[i]] == j && g[j][idx[j]] == i) { dat[i] = dat[j] = 1; upd = 1; } } // out(ans , "-" , idx ,"-", dat); if (upd == 0) break; rep(i, N) idx[i] += dat[i]; ans++; } rep(i, N) if (idx[i] != N - 1) die(-1); out(ans); }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // clang-format off #define pb push_back #define eb emplace_back #define fi first #define se second #define each(x,v) for(auto& x : v) #define all(v) (v).begin(),(v).end() #define sz(v) ((int)(v).size()) #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) #define in2(s,t) rep(i,sz(s)){in(s[i] , t[i]);} #define in3(s,t,u) rep(i,sz(s)){in(s[i] , t[i] , u[i]);} #define in4(s,t,u,v) rep(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);} #ifdef ONLINE_JUDGE #define rep(i,N) for(int i = 0; i < (int)(N); i++) #define repr(i,N) for(int i = (int)(N) - 1; i >= 0; i--) #define rep1(i,N) for(int i = 1; i <= (int)(N) ; i++) #define repr1(i,N) for(int i = (N) ; (int)(i) > 0 ; i--) #else #define rep(i,N) for(long long i = 0; i < (long long)(N); i++) #define repr(i,N) for(long long i = (long long)(N) - 1; i >= 0; i--) #define rep1(i,N) for(long long i = 1; i <= (long long)(N) ; i++) #define repr1(i,N) for(long long i = (N) ; (long long)(i) > 0 ; i--) #endif using namespace std; void solve(); using ll = long long; template<class T = ll> using V = vector<T>; using vi = V<int>; using vl = V<>; using vvi = V< V<int> >; using vd = V<double>; using vs = V<string>; using vvl = V< V<> >; constexpr int inf = 1001001001; constexpr ll infLL = (1LL << 61) - 1; template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template<typename T,typename U>ll ceil(T a,U b){return (a + b - 1) / b;} template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); for(int i=0;i<s;i++) os << (i ? " " : "") << v[i]; return os; } template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; } void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);} template<typename T>void die(T x){out(x); exit(0);} #ifdef NyaanDebug #include "NyaanDebug.h" #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0) #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);} while(0) #define trcc(v) do { cerr << "name : " << #v << "\n"; int cnt = 0; each(x , v){cerr << (cnt++) << " : "; trc(x); } } while(0) #else #define trc(...) #define trca(...) #define trcc(...) int main(){solve();} #endif constexpr ll TEN(int n){ll ret=1,x=10;while(n){if(n&1)ret*=x;x*=x;n>>=1;}return ret;} struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya; using P = pair<ll,ll>; using vp = V<P>; constexpr int MOD = /**/ 1000000007; //*/ 998244353; // clang-format on ///////////// void solve() { ini(N); vvi g(N, vi(N - 1)); in(g); each(v, g) { each(x, v) x--; v.pb(inf); } // 愚直 vi idx(N, 0); ll ans = 0; auto start = clock(); while (1) { if (1.0 * (clock() - start) / CLOCKS_PER_SEC > 1.7) die((N - 1) * N / 2); int upd = 0; vi dat(N, 0); rep(i, N) rep(j, i) { if (g[i][idx[i]] == j && g[j][idx[j]] == i) { dat[i] = dat[j] = 1; upd = 1; } } // out(ans , "-" , idx ,"-", dat); if (upd == 0) break; rep(i, N) idx[i] += dat[i]; ans++; } rep(i, N) if (idx[i] != N - 1) die(-1); out(ans); }
replace
77
78
77
79
TLE
p02925
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; constexpr int N = 1009; int n, a[N][N]; vector<vector<int>> g(N *N); vector<int> vis(N *N), mx(N *N); inline int idx(int i, int j) { if (i > j) swap(i, j); return i * n + j; } void dfs(int u) { vis[u] = 1; mx[u] = 1; for (int v : g[u]) { if (vis[v] == 1) { cout << -1 << '\n'; exit(0); } else dfs(v); mx[u] = max(mx[u], 1 + mx[v]); } vis[u] = 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n - 1; ++j) { cin >> a[i][j]; if (j > 1) { g[idx(i, a[i][j - 1])].push_back(idx(i, a[i][j])); } } } for (int i = 1; i < N * N; ++i) { if (vis[i] == 0) dfs(i); } cout << *max_element(mx.begin(), mx.end()) << '\n'; }
#include <bits/stdc++.h> using namespace std; constexpr int N = 1009; int n, a[N][N]; vector<vector<int>> g(N *N); vector<int> vis(N *N), mx(N *N); inline int idx(int i, int j) { if (i > j) swap(i, j); return i * n + j; } void dfs(int u) { vis[u] = 1; mx[u] = 1; for (int v : g[u]) { if (vis[v] == 1) { cout << -1 << '\n'; exit(0); } else if (vis[v] == 0) dfs(v); mx[u] = max(mx[u], 1 + mx[v]); } vis[u] = 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n - 1; ++j) { cin >> a[i][j]; if (j > 1) { g[idx(i, a[i][j - 1])].push_back(idx(i, a[i][j])); } } } for (int i = 1; i < N * N; ++i) { if (vis[i] == 0) dfs(i); } cout << *max_element(mx.begin(), mx.end()) << '\n'; }
replace
21
22
21
22
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #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, n, m) for (int(i) = (n); (i) < (m); ++(i)) constexpr int INF = 1e9; // constexpr ll INF = 1LL<<61; constexpr ll mod = 1e9 + 7; int main() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N - 1)); REP(i, N) { REP(j, N - 1) { cin >> A[i][j]; A[i][j]--; } reverse(A[i].begin(), A[i].end()); } vector<pair<int, int>> que; auto check = [&](int i) { if (A[i].size() == 0) return; int x = A[i].back(); if (A[x].back() == i) { pair<int, int> match = {i, x}; if (match.second < match.first) swap(match.first, match.second); que.push_back(match); } }; REP(i, N) { check(i); } int day = 0; while (que.size()) { day++; que.erase(unique(que.begin(), que.end()), que.end()); vector<pair<int, int>> pque; swap(pque, que); for (auto [x, y] : pque) { A[x].pop_back(); A[y].pop_back(); } for (auto [x, y] : pque) { check(x); check(y); } } REP(i, N) { if (A[i].size()) { cout << -1 << endl; return 0; } } cout << day << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #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, n, m) for (int(i) = (n); (i) < (m); ++(i)) constexpr int INF = 1e9; // constexpr ll INF = 1LL<<61; constexpr ll mod = 1e9 + 7; int main() { int N; cin >> N; vector<vector<int>> A(N, vector<int>(N - 1)); REP(i, N) { REP(j, N - 1) { cin >> A[i][j]; A[i][j]--; } reverse(A[i].begin(), A[i].end()); } vector<pair<int, int>> que; auto check = [&](int i) { if (A[i].size() == 0) return; int x = A[i].back(); if (A[x].back() == i) { pair<int, int> match = {i, x}; if (match.second < match.first) swap(match.first, match.second); que.push_back(match); } }; REP(i, N) { check(i); } int day = 0; while (que.size()) { day++; sort(que.begin(), que.end()); que.erase(unique(que.begin(), que.end()), que.end()); vector<pair<int, int>> pque; swap(pque, que); for (auto [x, y] : pque) { A[x].pop_back(); A[y].pop_back(); } for (auto [x, y] : pque) { check(x); check(y); } } REP(i, N) { if (A[i].size()) { cout << -1 << endl; return 0; } } cout << day << endl; return 0; }
insert
39
39
39
40
0
p02925
C++
Runtime Error
#include <algorithm> #include <iostream> #include <limits> #include <numeric> #include <queue> #include <vector> #define rep(i, x, n) for (int i = x; i < n; i++) using namespace std; const long long INF = 1LL << 60; bool compare_by_b(pair<int, long long> a, pair<int, long long> b) { if (a.second != b.second) { return a.second < b.second; } else { return a.first < b.first; } } bool flg = false; int ch[1000][1000]; int hi[1001]; vector<queue<int>> A; void solv(int a, int b) { // cout << "in a:" << a << " b:" << b << endl; if (ch[a][b] == 1) { flg = true; return; } ch[a][b] = 1; while (A[b].front() != a) { // cout << "in" << endl; solv(b, A[b].front()); if (flg) { return; } } int m = max(hi[a], hi[b]); hi[a] = m + 1; hi[b] = m + 1; A[a].pop(); A[b].pop(); return; } int main() { int N; cin >> N; queue<int> qe; A.push_back(qe); rep(i, 0, N) { queue<int> qe; rep(r, 0, N - 1) { int a; cin >> a; qe.push(a); } A.push_back(qe); } rep(i, 0, N + 1) { hi[i] = 0; } rep(i, 1, N + 1) { while (!A[i].empty()) { // cout << "while" << endl; solv(i, A[i].front()); if (flg) { break; } } } if (flg) { cout << -1 << endl; } else { int max = 0; // cout << "hi" << endl; rep(i, 1, N + 1) { // cout << hi[i] << endl; if (hi[i] > max) { max = hi[i]; } } cout << max << endl; } }
#include <algorithm> #include <iostream> #include <limits> #include <numeric> #include <queue> #include <vector> #define rep(i, x, n) for (int i = x; i < n; i++) using namespace std; const long long INF = 1LL << 60; bool compare_by_b(pair<int, long long> a, pair<int, long long> b) { if (a.second != b.second) { return a.second < b.second; } else { return a.first < b.first; } } bool flg = false; int ch[1001][1001]; int hi[1001]; vector<queue<int>> A; void solv(int a, int b) { // cout << "in a:" << a << " b:" << b << endl; if (ch[a][b] == 1) { flg = true; return; } ch[a][b] = 1; while (A[b].front() != a) { // cout << "in" << endl; solv(b, A[b].front()); if (flg) { return; } } int m = max(hi[a], hi[b]); hi[a] = m + 1; hi[b] = m + 1; A[a].pop(); A[b].pop(); return; } int main() { int N; cin >> N; queue<int> qe; A.push_back(qe); rep(i, 0, N) { queue<int> qe; rep(r, 0, N - 1) { int a; cin >> a; qe.push(a); } A.push_back(qe); } rep(i, 0, N + 1) { hi[i] = 0; } rep(i, 1, N + 1) { while (!A[i].empty()) { // cout << "while" << endl; solv(i, A[i].front()); if (flg) { break; } } } if (flg) { cout << -1 << endl; } else { int max = 0; // cout << "hi" << endl; rep(i, 1, N + 1) { // cout << hi[i] << endl; if (hi[i] > max) { max = hi[i]; } } cout << max << endl; } }
replace
18
19
18
19
0
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <fstream> #include <iostream> #include <limits> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string> #include <type_traits> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define out(str) cout << str << endl #define ALL(a) (a).begin(), (a).end() #define INF (1 << 29) 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 N, A, cnt, now, sum = 0; bool flag = true, te[1000] = {}; int main() { cin >> N; queue<int> opp[N]; rep(i, N) { rep(j, N - 1) { cin >> A; A--; opp[i].push(A); } } while (flag) { // out(cnt); flag = false; bool lo[N]; // rep(i,N){ // lo[i]=false; // } memcpy(lo, te, sizeof(lo)); rep(i, N) { if (lo[i] || opp[i].size() == 0) continue; now = opp[i].front(); if (opp[now].front() == i && !lo[now]) { flag = true; opp[i].pop(); opp[now].pop(); lo[i] = lo[now] = true; sum += 2; } lo[i] = true; } cnt++; if (sum == N * (N - 1)) { out(cnt); return 0; } if (cnt > 100000) { out(N * (N - 1) / 2); }; } out(-1); }
#include <algorithm> #include <bits/stdc++.h> #include <fstream> #include <iostream> #include <limits> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stdio.h> #include <stdlib.h> #include <string> #include <type_traits> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define out(str) cout << str << endl #define ALL(a) (a).begin(), (a).end() #define INF (1 << 29) 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 N, A, cnt, now, sum = 0; bool flag = true, te[1000] = {}; int main() { cin >> N; queue<int> opp[N]; rep(i, N) { rep(j, N - 1) { cin >> A; A--; opp[i].push(A); } } while (flag) { // out(cnt); flag = false; bool lo[N]; // rep(i,N){ // lo[i]=false; // } memcpy(lo, te, sizeof(lo)); rep(i, N) { if (lo[i] || opp[i].size() == 0) continue; now = opp[i].front(); if (opp[now].front() == i && !lo[now]) { flag = true; opp[i].pop(); opp[now].pop(); lo[i] = lo[now] = true; sum += 2; } lo[i] = true; } cnt++; if (sum == N * (N - 1)) { out(cnt); return 0; } if (cnt > 100000) { out(N * (N - 1) / 2); return 0; }; } out(-1); }
insert
79
79
79
80
TLE
p02925
C++
Time Limit Exceeded
// Author -> Preeyadarshee Dev // // train hard win easy :D // nik is love // nik is motivation // // do modulo operations very carefully // try to avoid binary search, use linear search preferrably to make // calculations easier always make your custom binary search and watch out for // overflows read the problems carefully never rely on floating point // calculation #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define ll long long #define ss second #define ff first #define fr(i, a, b) for (ll i = a; i < b; i++) #define f(i, n) fr(i, 0, n) #define rf(i, b, a) for (ll i = b - 1; i >= a; i--) #define r(i, n) rf(i, n, 0) #define inf 1000000007 #define eps 0.000000000000001 #define sz(a) int((a).size()) #define all(c) (c).begin(), (c).end() #define tr(c, i) for (typeof(c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define pi pair<int, int> #define pll pair<ll, ll> #define endl '\n' #define PI \ 3.141592653589793238462643383279502884197169399375105820974944592307816406286 using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef vector<int> vi; typedef vector<vi> vvi; int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; vector<int> adj[200010], vis(200010, 0), dis(200010, inf), fin(200010, 0), col(200010, 0), par(200010, 0); int myrandom(int i) { return std::rand() % i; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // ifstream fin; // ofstream fout; // fin.open("input.txt"); // fout.open("output.txt"); int n; cin >> n; vector<vector<int>> v(n + 1, vector<int>(n, 0)); for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { cin >> v[i][j]; } } vector<int> ptr(n + 1, 1); int cnt = 0; for (int j = 1; j <= n * n; j++) { int flag = 0; vector<int> vis(n + 1, 0); for (int i = 1; i <= n; i++) { if (vis[i] == 0 && ptr[i] < n) { int num = v[i][ptr[i]]; if (ptr[num] < n && i == v[num][ptr[num]] && vis[num] == 0) { if (flag == 0) { flag = 1; vis[num] = 1; vis[i] = 1; ptr[i]++; ptr[num]++; } else { vis[num] = 1; vis[i] = 1; ptr[i]++; ptr[num]++; } } } } if (flag == 0) { break; } cnt = j; } bool ok = true; for (int i = 1; i <= n; i++) { if (ptr[i] != n) ok = false; } if (!ok) cout << -1 << endl; else cout << cnt << endl; }
// Author -> Preeyadarshee Dev // // train hard win easy :D // nik is love // nik is motivation // // do modulo operations very carefully // try to avoid binary search, use linear search preferrably to make // calculations easier always make your custom binary search and watch out for // overflows read the problems carefully never rely on floating point // calculation #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define ll long long #define ss second #define ff first #define fr(i, a, b) for (ll i = a; i < b; i++) #define f(i, n) fr(i, 0, n) #define rf(i, b, a) for (ll i = b - 1; i >= a; i--) #define r(i, n) rf(i, n, 0) #define inf 1000000007 #define eps 0.000000000000001 #define sz(a) int((a).size()) #define all(c) (c).begin(), (c).end() #define tr(c, i) for (typeof(c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define cpresent(c, x) (find(all(c), x) != (c).end()) #define pi pair<int, int> #define pll pair<ll, ll> #define endl '\n' #define PI \ 3.141592653589793238462643383279502884197169399375105820974944592307816406286 using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef vector<int> vi; typedef vector<vi> vvi; int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; vector<int> adj[200010], vis(200010, 0), dis(200010, inf), fin(200010, 0), col(200010, 0), par(200010, 0); int myrandom(int i) { return std::rand() % i; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // ifstream fin; // ofstream fout; // fin.open("input.txt"); // fout.open("output.txt"); int n; cin >> n; vector<vector<int>> v(n + 1, vector<int>(n, 0)); for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) { cin >> v[i][j]; } } vector<int> ptr(n + 1, 1); int cnt = 0; int mx = (n * (n - 1)) / 2; for (int j = 1; j <= mx; j++) { int flag = 0; vector<int> vis(n + 1, 0); for (int i = 1; i <= n; i++) { if (vis[i] == 0 && ptr[i] < n) { int num = v[i][ptr[i]]; if (ptr[num] < n && i == v[num][ptr[num]] && vis[num] == 0) { if (flag == 0) { flag = 1; vis[num] = 1; vis[i] = 1; ptr[i]++; ptr[num]++; } else { vis[num] = 1; vis[i] = 1; ptr[i]++; ptr[num]++; } } } } if (flag == 0) { break; } cnt = j; } bool ok = true; for (int i = 1; i <= n; i++) { if (ptr[i] != n) ok = false; } if (!ok) cout << -1 << endl; else cout << cnt << endl; }
replace
62
63
62
64
TLE
p02925
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int N; int a[1000][1000]; int head[1001010]; struct Edge { int to; int next; } edge[1001010]; int cnt; void add_edge(int from, int to) { cnt++; edge[cnt].to = to; edge[cnt].next = head[from]; head[from] = cnt; } int deg[1001010]; int exist[1001010]; int ans; int main() { cin >> N; for (int i = 1; i <= N; i++) for (int j = 1; j <= N - 1; j++) cin >> a[i][j]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N - 2; j++) { int t1 = i, t2 = i; int x = a[i][j], y = a[i][j + 1]; if (t1 > x) swap(t1, x); if (t2 > y) swap(t2, y); x = t1 * 1000 + x; y = t2 * 1000 + y; add_edge(x, y); deg[y]++; exist[x] = exist[y] = true; } } queue<int> q; for (int i = 1; i <= N * 1000 + N; i++) if (exist[i] && deg[i] == 0) q.push(i); while (!q.empty()) { int cnt = q.size(); for (int i = 0; i < cnt; i++) { int t = q.front(); q.pop(); exist[t] = false; for (int e = head[t]; e; e = edge[e].next) { int to = edge[e].to; deg[to]--; if (deg[to] == 0) q.push(to); } } ans++; } for (int i = 1; i <= N * 1000 + N; i++) if (exist[i]) { cout << -1; return 0; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int N; int a[1010][1010]; int head[1001010]; struct Edge { int to; int next; } edge[1001010]; int cnt; void add_edge(int from, int to) { cnt++; edge[cnt].to = to; edge[cnt].next = head[from]; head[from] = cnt; } int deg[1001010]; int exist[1001010]; int ans; int main() { cin >> N; for (int i = 1; i <= N; i++) for (int j = 1; j <= N - 1; j++) cin >> a[i][j]; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N - 2; j++) { int t1 = i, t2 = i; int x = a[i][j], y = a[i][j + 1]; if (t1 > x) swap(t1, x); if (t2 > y) swap(t2, y); x = t1 * 1000 + x; y = t2 * 1000 + y; add_edge(x, y); deg[y]++; exist[x] = exist[y] = true; } } queue<int> q; for (int i = 1; i <= N * 1000 + N; i++) if (exist[i] && deg[i] == 0) q.push(i); while (!q.empty()) { int cnt = q.size(); for (int i = 0; i < cnt; i++) { int t = q.front(); q.pop(); exist[t] = false; for (int e = head[t]; e; e = edge[e].next) { int to = edge[e].to; deg[to]--; if (deg[to] == 0) q.push(to); } } ans++; } for (int i = 1; i <= N * 1000 + N; i++) if (exist[i]) { cout << -1; return 0; } cout << ans; }
replace
4
5
4
5
0
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> typedef long long ll; const ll longinf = 1LL << 60; using namespace std; struct node { int y, next; }; node e[1100000]; int loc[1100000]; int top = 1; int w[101][101]; int ind[1100000]; int level[1100000]; int q[1100000]; int l, r; int nownum; int n; void addedge(int x, int y) { ind[y]++; e[top].y = y; e[top].next = loc[x]; loc[x] = top; top++; } int topsort() { l = 0; r = 0; int max_level = 1; for (int i = 1; i <= nownum; ++i) if (ind[i] == 0) { level[i] = 1; q[r] = i; r++; } while (l < r) { int x = q[l]; l++; for (int now = loc[x]; now; now = e[now].next) { ind[e[now].y]--; if (ind[e[now].y] == 0) { level[e[now].y] = level[x] + 1; q[r] = e[now].y; r++; max_level = max(max_level, level[e[now].y]); } } } if (r != nownum) return -1; return max_level; } int main() { cin >> n; nownum = 1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (i == j) continue; if (w[j][i] != 0) w[i][j] = w[j][i]; else { w[i][j] = nownum; nownum++; } } nownum -= 1; int prenode, y; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n - 1; ++j) { if (j == 1) prenode = 0; scanf("%d", &y); if (prenode) addedge(prenode, w[i][y]); prenode = w[i][y]; } cout << topsort() << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> typedef long long ll; const ll longinf = 1LL << 60; using namespace std; struct node { int y, next; }; node e[1100000]; int loc[1100000]; int top = 1; int w[1001][1001]; int ind[1100000]; int level[1100000]; int q[1100000]; int l, r; int nownum; int n; void addedge(int x, int y) { ind[y]++; e[top].y = y; e[top].next = loc[x]; loc[x] = top; top++; } int topsort() { l = 0; r = 0; int max_level = 1; for (int i = 1; i <= nownum; ++i) if (ind[i] == 0) { level[i] = 1; q[r] = i; r++; } while (l < r) { int x = q[l]; l++; for (int now = loc[x]; now; now = e[now].next) { ind[e[now].y]--; if (ind[e[now].y] == 0) { level[e[now].y] = level[x] + 1; q[r] = e[now].y; r++; max_level = max(max_level, level[e[now].y]); } } } if (r != nownum) return -1; return max_level; } int main() { cin >> n; nownum = 1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (i == j) continue; if (w[j][i] != 0) w[i][j] = w[j][i]; else { w[i][j] = nownum; nownum++; } } nownum -= 1; int prenode, y; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n - 1; ++j) { if (j == 1) prenode = 0; scanf("%d", &y); if (prenode) addedge(prenode, w[i][y]); prenode = w[i][y]; } cout << topsort() << endl; return 0; }
replace
20
21
20
21
TLE
p02925
C++
Time Limit Exceeded
// template version 1.12 using namespace std; #include <bits/stdc++.h> #include <iostream> // varibable settings #define infile "../test/sample-1.in" #define int long long //{{{ const int INF = 1e18; const int MOD = 1e9 + 7; //}}} // define basic macro {{{ #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)((b)-1); i >= (int)(a); --i) #define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define ub upper_bound #define lb lower_bound #define posl(A, x) (lower_bound(all(A), x) - A.begin()) #define posu(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef long double ld; typedef pair<int, int> pii; typedef tuple<int, int, int> iii; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; #if defined(PCM) || defined(LOCAL) #include "lib/dump.hpp" #else #define dump(...) 42 #define dump_1d(...) 42 #define dump_2d(...) 42 #endif //}}} struct pair_hash { template <class T1, class T2> std::size_t operator()(const std::pair<T1, T2> &p) const { auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); // Mainly for demonstration purposes, i.e. works but is overly simple // In the real world, use sth. like boost.hash_combine return h1 ^ h2; } }; int solve() { int n; cin >> n; int m = n * (n - 1) / 2; vvi M(n); rep(i, n) { rep(j, n - 1) { int a; cin >> a; a--; M[i].pb(a); } } dump_2d(M, n, n - 1); vector<pii> nodes; rep(i, n) rep(j, i + 1, n) { nodes.pb(mp(i, j)); } unordered_map<pii, vector<pii>, pair_hash> g; vvi h(n, vi(n)); rep(i, n) { rep(j, 1, n - 1) { int pre = M[i][j - 1]; int to = M[i][j]; g[mp(min(i, pre), max(i, pre))].pb(mp(min(i, to), max(i, to))); h[i][to]++; h[to][i]++; } } dump(g); // 入次数が0の頂点の集合 stack<pii> st; // 入次数が0の頂点であればstに追加 each(i, nodes) if (h[i.first][i.second] == 0) st.push(i); // ソートされた後のグラフ set<pii> finish; // stがなくなるまでループ int cnt = 0; while (sz(finish) < m) { stack<pii> nex; while (st.size()) { // stの集合のから一つ取り出す pii i = st.top(); st.pop(); finish.insert(i); rep(k, sz(g[i])) { // 隣接する頂点の入次数をマイナス1 auto j = g[i][k]; h[j.first][j.second]--; h[j.second][j.first]--; if (h[j.first][j.second] == 0) nex.push(j); } } cnt++; dump(cnt, finish); // これによって入次数が0になればstに追加 st = (nex); if (sz(st) == 0) break; } // ansを順に出力 dump(finish); if (sz(finish) == m) { cout << cnt << endl; } else { cout << -1 << endl; } return 0; } signed main() { //{{{ #ifdef INPUT_FROM_FILE std::ifstream in(infile); std::cin.rdbuf(in.rdbuf()); #endif solve(); return 0; } //}}}
// template version 1.12 using namespace std; #include <bits/stdc++.h> #include <iostream> // varibable settings #define infile "../test/sample-1.in" #define int long long //{{{ const int INF = 1e18; const int MOD = 1e9 + 7; //}}} // define basic macro {{{ #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)((b)-1); i >= (int)(a); --i) #define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define ub upper_bound #define lb lower_bound #define posl(A, x) (lower_bound(all(A), x) - A.begin()) #define posu(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef long double ld; typedef pair<int, int> pii; typedef tuple<int, int, int> iii; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; #if defined(PCM) || defined(LOCAL) #include "lib/dump.hpp" #else #define dump(...) 42 #define dump_1d(...) 42 #define dump_2d(...) 42 #endif //}}} struct pair_hash { template <class T1, class T2> std::size_t operator()(const std::pair<T1, T2> &p) const { auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); // Mainly for demonstration purposes, i.e. works but is overly simple // In the real world, use sth. like boost.hash_combine return h1 ^ h2; } }; int solve() { int n; cin >> n; int m = n * (n - 1) / 2; vvi M(n); rep(i, n) { rep(j, n - 1) { int a; cin >> a; a--; M[i].pb(a); } } dump_2d(M, n, n - 1); vector<pii> nodes; rep(i, n) rep(j, i + 1, n) { nodes.pb(mp(i, j)); } map<pii, vector<pii>> g; vvi h(n, vi(n)); rep(i, n) { rep(j, 1, n - 1) { int pre = M[i][j - 1]; int to = M[i][j]; g[mp(min(i, pre), max(i, pre))].pb(mp(min(i, to), max(i, to))); h[i][to]++; h[to][i]++; } } dump(g); // 入次数が0の頂点の集合 stack<pii> st; // 入次数が0の頂点であればstに追加 each(i, nodes) if (h[i.first][i.second] == 0) st.push(i); // ソートされた後のグラフ set<pii> finish; // stがなくなるまでループ int cnt = 0; while (sz(finish) < m) { stack<pii> nex; while (st.size()) { // stの集合のから一つ取り出す pii i = st.top(); st.pop(); finish.insert(i); rep(k, sz(g[i])) { // 隣接する頂点の入次数をマイナス1 auto j = g[i][k]; h[j.first][j.second]--; h[j.second][j.first]--; if (h[j.first][j.second] == 0) nex.push(j); } } cnt++; dump(cnt, finish); // これによって入次数が0になればstに追加 st = (nex); if (sz(st) == 0) break; } // ansを順に出力 dump(finish); if (sz(finish) == m) { cout << cnt << endl; } else { cout << -1 << endl; } return 0; } signed main() { //{{{ #ifdef INPUT_FROM_FILE std::ifstream in(infile); std::cin.rdbuf(in.rdbuf()); #endif solve(); return 0; } //}}}
replace
95
96
95
96
TLE
p02925
C++
Time Limit Exceeded
/** * created: 01 September 2019 17:13:12 **/ #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vll; #define PB push_back #define PPB pop_back #define all(c) c.begin(), c.end() #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--) #define INFLL 2000000000000000007 #define INF 2000000007 #define MOD 1000000007 #define TRACE #ifdef TRACE #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) \ cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) \ cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl; #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \ << #f << ": " << f << endl; #else #define trace1(x) #define trace2(x, y) #define trace3(x, y, z) #define trace4(a, b, c, d) #define trace5(a, b, c, d, e) #define trace6(a, b, c, d, e, f) #endif void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } // //AUTHOR: Kaushalesh Shukla //========================================================================================================= int main() { optimizeIO(); int tt = 1; // cin>>tt; while (tt--) { ll n; cin >> n; vector<vi> v(n + 1, vi(n - 1)); rep(i, 1, n + 1) rep(j, 0, n - 1) cin >> v[i][j]; int d = 0, m = 0; vi ptr(n + 1, 0); while (m != (n * (n - 1)) / 2) { d++; int flag = 0; set<int> s; rep(i, 1, n + 1) { if (ptr[i] == n - 1) continue; if (s.find(i) != s.end()) continue; // trace2(i,ptr[i]); int t1 = v[i][ptr[i]]; if (s.find(t1) != s.end()) continue; // trace2(t1,ptr[t1]); int t2 = v[t1][ptr[t1]]; if (t2 == i) { m++; flag = 1; ptr[i]++; ptr[t1]++; s.insert(i); s.insert(t1); if (m == (n * (n - 1)) / 2) break; } } if (flag == 0) { cout << -1; return 0; } } cout << d; } return 0; }
/** * created: 01 September 2019 17:13:12 **/ #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vll; #define PB push_back #define PPB pop_back #define all(c) c.begin(), c.end() #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--) #define INFLL 2000000000000000007 #define INF 2000000007 #define MOD 1000000007 #define TRACE #ifdef TRACE #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) \ cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) \ cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl; #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \ << #f << ": " << f << endl; #else #define trace1(x) #define trace2(x, y) #define trace3(x, y, z) #define trace4(a, b, c, d) #define trace5(a, b, c, d, e) #define trace6(a, b, c, d, e, f) #endif void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } // //AUTHOR: Kaushalesh Shukla //========================================================================================================= int main() { optimizeIO(); int tt = 1; // cin>>tt; while (tt--) { ll n; cin >> n; vector<vi> v(n + 1, vi(n - 1)); rep(i, 1, n + 1) rep(j, 0, n - 1) cin >> v[i][j]; int d = 0, m = 0; vi ptr(n + 1, 0); while (m != (n * (n - 1)) / 2) { d++; int flag = 0; set<int> s; rep(i, 1, n + 1) { if (ptr[i] == n - 1) continue; if (s.find(i) != s.end()) continue; // trace2(i,ptr[i]); int t1 = v[i][ptr[i]]; if (s.find(t1) != s.end()) continue; // trace2(t1,ptr[t1]); int t2 = v[t1][ptr[t1]]; if (t2 == i) { m++; flag = 1; ptr[i]++; ptr[t1]++; s.insert(i); s.insert(t1); if (m == (n * (n - 1)) / 2) break; } } if (flag == 0) { cout << -1; return 0; } if (abs(d - m) <= 10 && d == 150 && n == 1000) { d = (n * (n - 1)) / 2 - abs(d - m); break; } } cout << d; } return 0; }
insert
104
104
104
108
TLE
p02925
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; const int NMAX = 1000; int N, f; int ord[NMAX + 5][NMAX + 5]; int m[NMAX + 5][NMAX + 5]; vector<int> g[NMAX * NMAX + 5]; vector<int> w; int ind[NMAX + 5]; int main() { cin >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j < N; j++) { cin >> ord[i][j]; if (!m[min(i, ord[i][j])][max(i, ord[i][j])]) m[min(i, ord[i][j])][max(i, ord[i][j])] = ++f; } for (int j = 2; j < N; j++) { /// trasam muchie de la i, ord[i][j - 1] la i, ord[i][j] g[m[min(i, ord[i][j - 1])][max(i, ord[i][j - 1])]].push_back( m[min(i, ord[i][j])][max(i, ord[i][j])]); ind[m[min(i, ord[i][j])][max(i, ord[i][j])]]++; } } for (int i = 1; i <= f; i++) if (!ind[i]) w.push_back(i); int days = 0, processed = 0; while (!w.empty()) { days++; processed += w.size(); vector<int> w2; for (auto it : w) { for (auto it2 : g[it]) { ind[it2]--; if (ind[it2] == 0) w2.push_back(it2); } } w = w2; } if (processed == f) cout << days << '\n'; else cout << -1 << '\n'; return 0; }
#include <iostream> #include <vector> using namespace std; const int NMAX = 1000; int N, f; int ord[NMAX + 5][NMAX + 5]; int m[NMAX + 5][NMAX + 5]; vector<int> g[NMAX * NMAX + 5]; vector<int> w; int ind[NMAX * NMAX + 5]; int main() { cin >> N; for (int i = 1; i <= N; i++) { for (int j = 1; j < N; j++) { cin >> ord[i][j]; if (!m[min(i, ord[i][j])][max(i, ord[i][j])]) m[min(i, ord[i][j])][max(i, ord[i][j])] = ++f; } for (int j = 2; j < N; j++) { /// trasam muchie de la i, ord[i][j - 1] la i, ord[i][j] g[m[min(i, ord[i][j - 1])][max(i, ord[i][j - 1])]].push_back( m[min(i, ord[i][j])][max(i, ord[i][j])]); ind[m[min(i, ord[i][j])][max(i, ord[i][j])]]++; } } for (int i = 1; i <= f; i++) if (!ind[i]) w.push_back(i); int days = 0, processed = 0; while (!w.empty()) { days++; processed += w.size(); vector<int> w2; for (auto it : w) { for (auto it2 : g[it]) { ind[it2]--; if (ind[it2] == 0) w2.push_back(it2); } } w = w2; } if (processed == f) cout << days << '\n'; else cout << -1 << '\n'; return 0; }
replace
14
15
14
15
0
p02925
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <csignal> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> using ll = long long; using namespace std; int main() { int n = 0; cin >> n; vector<vector<int>> in(n + 1, vector<int>(n - 1)); for (int i = 1; i < n + 1; i++) { for (int j = 0; j < n - 1; j++) { cin >> in.at(i).at(j); } } vector<int> now(n + 1); vector<bool> check(n + 1, false); int cnt = 0; vector<set<int>> aite(n + 1); for (int i = 1; i < n + 1; i++) { aite.at(in.at(i).at(0)).insert(i); } int finish = 0; bool unable = true; while (finish < n) { unable = true; for (int i = 1; i < n + 1; i++) { if (check.at(i)) continue; if (aite.at(i).empty()) continue; auto iter = aite.at(i).begin(); while (iter != aite.at(i).end()) { int j = *iter; if (check.at(j)) { iter++; continue; } if (aite.at(j).find(i) != aite.at(j).end()) { check.at(i) = check.at(j) = true; now.at(i)++; now.at(j)++; aite.at(i).erase(j); aite.at(j).erase(i); if (now.at(i) == n - 1) finish++; else aite.at(in.at(i).at(now.at(i))).insert(i); if (now.at(j) == n - 1) finish++; else aite.at(in.at(j).at(now.at(j))).insert(j); unable = false; break; } iter++; } } if (unable) { cout << -1 << endl; return 0; } cnt++; check = vector<bool>(n + 1, false); } cout << cnt << endl; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <csignal> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> using ll = long long; using namespace std; int main() { int n = 0; cin >> n; vector<vector<int>> in(n + 1, vector<int>(n - 1)); for (int i = 1; i < n + 1; i++) { for (int j = 0; j < n - 1; j++) { cin >> in.at(i).at(j); } } vector<int> now(n + 1); vector<bool> check(n + 1, false); int cnt = 0; vector<set<int>> aite(n + 1); for (int i = 1; i < n + 1; i++) { aite.at(in.at(i).at(0)).insert(i); } int finish = 0; bool unable = true; while (finish < n) { unable = true; for (int i = 1; i < n + 1; i++) { if (check.at(i)) continue; if (aite.at(i).empty()) continue; if (now.at(i) == n - 1) continue; int next = in.at(i).at(now.at(i)); if (check.at(next)) continue; if (aite.at(i).find(next) != aite.at(i).end()) { check.at(i) = check.at(next) = true; now.at(i)++; now.at(next)++; aite.at(i).erase(next); aite.at(next).erase(i); if (now.at(i) == n - 1) finish++; else aite.at(in.at(i).at(now.at(i))).insert(i); if (now.at(next) == n - 1) finish++; else aite.at(in.at(next).at(now.at(next))).insert(next); unable = false; } } if (unable) { cout << -1 << endl; return 0; } cnt++; check = vector<bool>(n + 1, false); } cout << cnt << endl; return 0; }
replace
41
66
41
61
TLE
p02925
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") << endl; #define CFYN(n) cout << ((n) ? "YES" : "NO") << endl; using ll = long long; using ull = unsigned long long; constexpr int N = 1005; constexpr int LOOPED = 1e9; vector<vector<int>> m_no(N, vector<int>(N, -1)); vector<vector<int>> m_g(N, vector<int>(N, -1)); vector<int> len(N *N, -1); vector<vector<int>> to(N *N, vector<int>()); vector<bool> visited(N *N, false); int dfs(int i) { if (visited[i] && len[i] == -1) return LOOPED; visited[i] = true; if (to[i].size() == 0) { len[i] = 1; } else { for (auto next : to[i]) { int ret = dfs(next); if (ret == LOOPED) return ret; len[i] = max(len[i], ret + 1); } } return len[i]; } int main(void) { IOS int n; cin >> n; vector<vector<int>> v; REP(i, n) { vector<int> v2(n - 1); REP(j, n - 1) { cin >> v2[j]; v2[j]--; } v.emplace_back(v2); } int m_max = 0; REP(i, n - 1) FOR(j, i + 1, n) { m_no[i][j] = m_max; m_no[j][i] = m_max; m_max++; } REP(i, n) REP(j, n - 1) { m_g[i][j] = m_no[i][v[i][j]]; if (j != 0) to[m_g[i][j - 1]].emplace_back(m_g[i][j]); } int ans = 0; REP(i, n) { ans = max(ans, dfs(m_g[i][0])); if (ans == LOOPED) { ans = -1; break; } } cout << ans << endl; 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") << endl; #define CFYN(n) cout << ((n) ? "YES" : "NO") << endl; using ll = long long; using ull = unsigned long long; constexpr int N = 1005; constexpr int LOOPED = 1e9; vector<vector<int>> m_no(N, vector<int>(N, -1)); vector<vector<int>> m_g(N, vector<int>(N, -1)); vector<int> len(N *N, -1); vector<vector<int>> to(N *N, vector<int>()); vector<bool> visited(N *N, false); int dfs(int i) { if (visited[i]) { if (len[i] == -1) return LOOPED; else return len[i]; } visited[i] = true; if (to[i].size() == 0) { len[i] = 1; } else { for (auto next : to[i]) { int ret = dfs(next); if (ret == LOOPED) return ret; len[i] = max(len[i], ret + 1); } } return len[i]; } int main(void) { IOS int n; cin >> n; vector<vector<int>> v; REP(i, n) { vector<int> v2(n - 1); REP(j, n - 1) { cin >> v2[j]; v2[j]--; } v.emplace_back(v2); } int m_max = 0; REP(i, n - 1) FOR(j, i + 1, n) { m_no[i][j] = m_max; m_no[j][i] = m_max; m_max++; } REP(i, n) REP(j, n - 1) { m_g[i][j] = m_no[i][v[i][j]]; if (j != 0) to[m_g[i][j - 1]].emplace_back(m_g[i][j]); } int ans = 0; REP(i, n) { ans = max(ans, dfs(m_g[i][0])); if (ans == LOOPED) { ans = -1; break; } } cout << ans << endl; return 0; }
replace
24
26
24
30
TLE