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
p03137
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> A(M, 0); for (int i = 0; i < M; i++) cin >> A[i]; sort(A.begin(), A.end()); vector<int> B(M - 1, 0); for (int i = 0; i < M - 1; i++) { B[i] = A[i + 1] - A[i]; } sort(B.begin(), B.end(), greater<int>()); int ans = A[M - 1] - A[0]; for (int i = 0; i < N - 1; i++) { ans -= B[i]; } cout << ans; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; if (N > M) { cout << 0; return 0; } vector<int> A(M, 0); for (int i = 0; i < M; i++) cin >> A[i]; sort(A.begin(), A.end()); vector<int> B(M - 1, 0); for (int i = 0; i < M - 1; i++) { B[i] = A[i + 1] - A[i]; } sort(B.begin(), B.end(), greater<int>()); int ans = A[M - 1] - A[0]; for (int i = 0; i < N - 1; i++) { ans -= B[i]; } cout << ans; }
insert
9
9
9
14
0
p03137
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> X, dis_X; for (int i = 0; i < M; i++) { int element; cin >> element; X.push_back(element); } if (M == 1) { cout << 0 << endl; } else if (N > M) { cout << 0 << endl; } else { sort(X.begin(), X.end()); int first, last; first = X[0]; last = X[X.size() - 1]; int distance = last - first; for (int i = 1; i < M; i++) { int dis = X[i] - X[i - 1]; dis_X.push_back(dis); } sort(dis_X.begin(), dis_X.end()); int count = 0; for (int i = 0; i <= dis_X.size() - N; i++) { count += dis_X[i]; } cout << count << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> X, dis_X; for (int i = 0; i < M; i++) { int element; cin >> element; X.push_back(element); } if (M == 1) { cout << 0 << endl; } else if (N >= M) { cout << 0 << endl; } else { sort(X.begin(), X.end()); int first, last; first = X[0]; last = X[X.size() - 1]; int distance = last - first; for (int i = 1; i < M; i++) { int dis = X[i] - X[i - 1]; dis_X.push_back(dis); } sort(dis_X.begin(), dis_X.end()); int count = 0; for (int i = 0; i <= dis_X.size() - N; i++) { count += dis_X[i]; } cout << count << endl; } return 0; }
replace
16
17
16
17
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = int(a); i < int(b); ++i) #define SIZE 200005 #define INF 1000000005LL #define MOD 1000000007 using namespace std; typedef long long int ll; typedef pair<int, int> P; int main() { int n, m; cin >> n >> m; vector<int> data(m); rep(i, 0, m) { cin >> data[i]; } if (m == 1) { cout << 0 << endl; return 0; } sort(data.begin(), data.end()); vector<int> distance(m - 1); rep(i, 0, m - 1) { distance[i] = data[i + 1] - data[i]; } sort(distance.begin(), distance.end()); reverse(distance.begin(), distance.end()); int total = data[m - 1] - data[0]; rep(i, 0, n - 1) { total -= distance[i]; } cout << total << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = int(a); i < int(b); ++i) #define SIZE 200005 #define INF 1000000005LL #define MOD 1000000007 using namespace std; typedef long long int ll; typedef pair<int, int> P; int main() { int n, m; cin >> n >> m; vector<int> data(m); rep(i, 0, m) { cin >> data[i]; } if (m == 1) { cout << 0 << endl; return 0; } if (n >= m) { cout << 0 << endl; return 0; } sort(data.begin(), data.end()); vector<int> distance(m - 1); rep(i, 0, m - 1) { distance[i] = data[i + 1] - data[i]; } sort(distance.begin(), distance.end()); reverse(distance.begin(), distance.end()); int total = data[m - 1] - data[0]; rep(i, 0, n - 1) { total -= distance[i]; } cout << total << endl; return 0; }
insert
18
18
18
22
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, m; cin >> n >> m; if (m == 1) { cout << 0 << endl; return 0; } vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i]; sort(a.begin(), a.end()); vector<int> div(m - 1); for (int i = 0; i < m - 1; i++) { div[i] = a[i + 1] - a[i]; } sort(div.begin(), div.end()); ll ans = 0; for (int i = 0; i < div.size() - (n - 1); i++) { ans += div[i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, m; cin >> n >> m; if (m == 1) { cout << 0 << endl; return 0; } vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i]; sort(a.begin(), a.end()); vector<int> div(m - 1); for (int i = 0; i < m - 1; i++) { div[i] = a[i + 1] - a[i]; } sort(div.begin(), div.end()); ll ans = 0; for (int i = 0; i < max(0, (int)div.size() - (n - 1)); i++) { if (i < div.size()) ans += div[i]; } cout << ans << endl; return 0; }
replace
21
23
21
24
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define PI 3.14159265358979323846 constexpr int INF = numeric_limits<int>::max() / 2; constexpr long long INFL = numeric_limits<long long>::max() / 2; constexpr int MOD = 1000000007; using Graph = vector<vector<int>>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; int X[M]; rep(i, M) cin >> X[i]; sort(X, X + M); int x[M - 1]; rep(i, M - 1) { x[i] = X[i + 1] - X[i]; } int ans = X[M - 1] - X[0]; sort(x, x + M - 1); reverse(x, x + M - 1); rep(i, N - 1) { ans -= x[i]; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define PI 3.14159265358979323846 constexpr int INF = numeric_limits<int>::max() / 2; constexpr long long INFL = numeric_limits<long long>::max() / 2; constexpr int MOD = 1000000007; using Graph = vector<vector<int>>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; int X[M]; rep(i, M) cin >> X[i]; int ans = 0; if (N >= M) ans = 0; else { sort(X, X + M); int x[M - 1]; rep(i, M - 1) { x[i] = X[i + 1] - X[i]; } ans = X[M - 1] - X[0]; sort(x, x + M - 1); reverse(x, x + M - 1); rep(i, N - 1) { ans -= x[i]; } } cout << ans << endl; }
replace
18
25
18
30
0
p03137
C++
Runtime Error
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <numeric> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using lli = long long int; template <class T, class U> void init_n(vector<T> &v, size_t n, U x) { v = vector<T>(n, x); } template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); } template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) { v = vector<T>(n + o); for (lli i = o; i < n + o; ++i) cin >> v[i]; } template <class T> T gabs(const T &x) { return max(x, -x); } #define abs gabs lli n, m; vector<lli> x; int main() { cin >> n >> m; read_n(x, m); if (m == 1) { cout << 0 << endl; return 0; } sort(begin(x), end(x)); vector<lli> d(m - 1); for (lli i = 0; i < m; ++i) { d[i] = x[i + 1] - x[i]; } sort(begin(d), end(d)); lli ans = 0; for (lli i = 0; i < m - n; ++i) ans += d[i]; cout << ans << endl; return 0; }
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <numeric> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using lli = long long int; template <class T, class U> void init_n(vector<T> &v, size_t n, U x) { v = vector<T>(n, x); } template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); } template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) { v = vector<T>(n + o); for (lli i = o; i < n + o; ++i) cin >> v[i]; } template <class T> T gabs(const T &x) { return max(x, -x); } #define abs gabs lli n, m; vector<lli> x; int main() { cin >> n >> m; read_n(x, m); if (m == 1) { cout << 0 << endl; return 0; } sort(begin(x), end(x)); vector<lli> d(m - 1); for (lli i = 0; i < m - 1; ++i) { d[i] = x[i + 1] - x[i]; } sort(begin(d), end(d)); lli ans = 0; for (lli i = 0; i < m - n; ++i) ans += d[i]; cout << ans << endl; return 0; }
replace
41
42
41
42
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> x(m); for (int i = 0; i < m; ++i) cin >> x[i]; sort(x.begin(), x.end()); vector<int> diff(m - 1); for (int i = 0; i < m; ++i) diff[i] = x[i + 1] - x[i]; sort(diff.begin(), diff.end()); int ans = 0; for (int i = 0; i < m - n; ++i) ans += diff[i]; if (n < m) cout << ans << endl; else cout << 0 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> x(m); for (int i = 0; i < m; ++i) cin >> x[i]; sort(x.begin(), x.end()); vector<int> diff(m - 1); for (int i = 0; i < m - 1; ++i) diff[i] = x[i + 1] - x[i]; sort(diff.begin(), diff.end()); int ans = 0; for (int i = 0; i < m - n; ++i) ans += diff[i]; if (n < m) cout << ans << endl; else cout << 0 << endl; }
replace
13
14
13
14
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for (int i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--) #define IREP(i, n) IFOR(i, 0, n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(), v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL << (n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) \ { \ cout << #v << "=" << endl; \ REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \ cout << endl; \ } #define mdebug(m) \ { \ cout << #m << "=" << endl; \ REP(i_debug, m.size()) { \ REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \ cout << endl; \ } \ } #define Return(ans) \ { \ cout << (ans) << endl; \ return 0; \ } #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (int i = 0; i < v.size(); i++) { cout << v[i]; if (i != v.size() - 1) cout << endl; }; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { cout << '(' << p.first << ',' << p.second << ')'; return os; } template <typename T> void Out(T x) { cout << x << endl; } template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if (f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; // int dx[4] = {1,0,-1,0}; // int dy[4] = {0,1,0,-1}; // char d[4] = {'D','R','U','L'}; const int mod = 1000000007; // const int mod = 998244353; // #define Add(x, y) x = (x + (y)) % mod // #define Mult(x, y) x = (x * (y)) % mod #include <bits/stdc++.h> using namespace std; signed main() { int N, M; cin >> N >> M; vec X(N); REP(i, N) cin >> X[i]; Sort(X); vec D(M - 1); REP(i, M - 1) D[i] = X[i + 1] - X[i]; Sort(D); int ans = 0; REP(i, M - N) ans += D[i]; Out(ans); }
#include <bits/stdc++.h> using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/ struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for (int i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--) #define IREP(i, n) IFOR(i, 0, n) #define Sort(v) sort(v.begin(), v.end()) #define Reverse(v) reverse(v.begin(), v.end()) #define all(v) v.begin(), v.end() #define SZ(v) ((int)v.size()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) #define bit(n) (1LL << (n)) #define bit_exist(x, n) ((x >> n) & 1) #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) \ { \ cout << #v << "=" << endl; \ REP(i_debug, v.size()) { cout << v[i_debug] << ","; } \ cout << endl; \ } #define mdebug(m) \ { \ cout << #m << "=" << endl; \ REP(i_debug, m.size()) { \ REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; } \ cout << endl; \ } \ } #define Return(ans) \ { \ cout << (ans) << endl; \ return 0; \ } #define pb push_back #define f first #define s second #define int long long #define INF 1000000000000000000 template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (int i = 0; i < v.size(); i++) { cout << v[i]; if (i != v.size() - 1) cout << endl; }; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { cout << '(' << p.first << ',' << p.second << ')'; return os; } template <typename T> void Out(T x) { cout << x << endl; } template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if (f) Out(y); else Out(n); } using vec = vector<int>; using mat = vector<vec>; using Pii = pair<int, int>; using PiP = pair<int, Pii>; using PPi = pair<Pii, int>; using bools = vector<bool>; using pairs = vector<Pii>; // int dx[4] = {1,0,-1,0}; // int dy[4] = {0,1,0,-1}; // char d[4] = {'D','R','U','L'}; const int mod = 1000000007; // const int mod = 998244353; // #define Add(x, y) x = (x + (y)) % mod // #define Mult(x, y) x = (x * (y)) % mod #include <bits/stdc++.h> using namespace std; signed main() { int N, M; cin >> N >> M; vec X(M); REP(i, M) cin >> X[i]; Sort(X); vec D(M - 1); REP(i, M - 1) D[i] = X[i + 1] - X[i]; Sort(D); int ans = 0; REP(i, M - N) ans += D[i]; Out(ans); }
replace
107
109
107
109
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int N, M; cin >> N >> M; if (M == 1) { cout << "0" << "\n"; return 0; } vector<int> v(M); for (int i = 0; i < M; i++) { cin >> v.at(i); } sort(v.begin(), v.end()); vector<int> sa(M - 1); for (int i = 1; i < M; i++) { sa.at(i - 1) = v.at(i) - v.at(i - 1); } sort(sa.begin(), sa.end()); reverse(sa.begin(), sa.end()); int soukyori = v.at(M - 1) - v.at(0); int yaranai = 0; for (int i = 0; i < N - 1; i++) { yaranai += sa.at(i); } cout << soukyori - yaranai << "\n"; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int N, M; cin >> N >> M; if (M == 1 || N >= M) { cout << "0" << "\n"; return 0; } vector<int> v(M); for (int i = 0; i < M; i++) { cin >> v.at(i); } sort(v.begin(), v.end()); vector<int> sa(M - 1); for (int i = 1; i < M; i++) { sa.at(i - 1) = v.at(i) - v.at(i - 1); } sort(sa.begin(), sa.end()); reverse(sa.begin(), sa.end()); int soukyori = v.at(M - 1) - v.at(0); int yaranai = 0; for (int i = 0; i < N - 1; i++) { yaranai += sa.at(i); } cout << soukyori - yaranai << "\n"; }
replace
7
8
7
8
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int(i) = 0; i < (n); i++) int main() { int n, m, ans = 0; cin >> n >> m; vector<int> x(m, 0), d(m - 1, 0); REP(i, m) cin >> x.at(i); sort(x.begin(), x.end()); REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i); sort(d.begin(), d.end()); cout << (m == 1 ? 0 : accumulate(d.begin(), d.begin() + m - n, 0)); }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int(i) = 0; i < (n); i++) int main() { int n, m, ans = 0; cin >> n >> m; vector<int> x(m, 0), d(m - 1, 0); REP(i, m) cin >> x.at(i); sort(x.begin(), x.end()); REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i); sort(d.begin(), d.end()); cout << (m == 1 ? 0 : accumulate(d.begin(), d.begin() + m - (min(n, m)), 0)); }
replace
12
13
12
13
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int(i) = 0; i < (n); i++) int main() { int n, m, ans = 0; cin >> n >> m; vector<int> x(m, 0), d(m - 1, 0); REP(i, m) cin >> x.at(i); sort(x.begin(), x.end()); REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i); sort(d.begin(), d.end(), greater<int>()); ans = accumulate(d.begin(), d.end(), 0); REP(i, n - 1) ans -= d.at(i); cout << ans; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int(i) = 0; i < (n); i++) int main() { int n, m, ans = 0; cin >> n >> m; if (m != 1) { vector<int> x(m, 0), d(m - 1, 0); REP(i, m) cin >> x.at(i); sort(x.begin(), x.end()); REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i); sort(d.begin(), d.end(), greater<int>()); ans = accumulate(d.begin(), d.end(), 0); for (int i = 0; (i < n - 1) && (i < m - 1); i++) ans -= d.at(i); } cout << ans; }
replace
7
14
7
17
0
p03137
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; int main() { long int n, m, x[100000], min, tmp, sum; cin >> n >> m; if (n >= m) { cout << 0 << endl; return 0; } cin >> x[0]; for (int i = 1; i < m; i++) { cin >> x[i]; } /* for(int i=0;i<m;i++){ min = i; for(int k=i+1;k<m;k++){ if(x[min]>x[k])min=k; } tmp = x[min]; x[min] = x[i]; x[i] = tmp; } */ sort(x, x + m); sum = x[m - 1] - x[0]; for (int i = 0; i < m - 1; i++) { x[i] = x[i + 1] - x[i]; } for (int i = 0; i < m - 1; i++) { min = i; for (int k = i + 1; k < m - 1; k++) { if (x[min] > x[k]) min = k; } tmp = x[min]; x[min] = x[i]; x[i] = tmp; } for (int i = 1; i <= n - 1; i++) { sum -= x[m - 1 - i]; } cout << sum << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { long int n, m, x[100000], min, tmp, sum; cin >> n >> m; if (n >= m) { cout << 0 << endl; return 0; } cin >> x[0]; for (int i = 1; i < m; i++) { cin >> x[i]; } /* for(int i=0;i<m;i++){ min = i; for(int k=i+1;k<m;k++){ if(x[min]>x[k])min=k; } tmp = x[min]; x[min] = x[i]; x[i] = tmp; } */ sort(x, x + m); sum = x[m - 1] - x[0]; for (int i = 0; i < m - 1; i++) { x[i] = x[i + 1] - x[i]; } /* for(int i=0;i<m-1;i++){ min = i; for(int k=i+1;k<m-1;k++){ if(x[min]>x[k])min=k; } tmp = x[min]; x[min] = x[i]; x[i] = tmp; }*/ sort(x, x + m - 1); for (int i = 1; i <= n - 1; i++) { sum -= x[m - 1 - i]; } cout << sum << endl; return 0; }
replace
30
40
30
41
TLE
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> l; for (int i = 0; i < m; i++) { int a; cin >> a; l.push_back(a); } sort(l.begin(), l.end()); vector<int> d_l; for (int i = 0; i < m - 1; i++) { d_l.push_back(l[i + 1] - l[i]); } sort(d_l.begin(), d_l.end()); int sum = 0; for (int i = 0; i < n - 1; i++) { sum += d_l[m - 2 - i]; } int l_last = l[m - 1] - l[0]; cout << l_last - sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n > m) n = m; vector<int> l; for (int i = 0; i < m; i++) { int a; cin >> a; l.push_back(a); } sort(l.begin(), l.end()); vector<int> d_l; for (int i = 0; i < m - 1; i++) { d_l.push_back(l[i + 1] - l[i]); } sort(d_l.begin(), d_l.end()); int sum = 0; for (int i = 0; i < n - 1; i++) { sum += d_l[m - 2 - i]; } int l_last = l[m - 1] - l[0]; cout << l_last - sum << endl; return 0; }
insert
7
7
7
9
0
p03137
C++
Runtime Error
/*Function Template*/ #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; #define rep(i, n) for (long long int i = 0; i < (n); i++) int Len(int n) { int s = 0; while (n != 0) s++, n /= 10; return s; } int Sint(int n) { int m = 0, s = 0, a = n; while (a != 0) s++, a /= 10; for (int i = s - 1; i >= 0; i--) m += n / ((int)pow(10, i)) - (n / ((int)pow(10, i + 1))) * 10; return m; } long long int Svec(vector<long long int> v) { long long int n = 0; for (long long int i = 0; i < v.size(); i++) n += v[i]; return n; } int GCD(int a, int b) { int r, tmp; /*自然数 a > b を確認・入替*/ if (a < b) { tmp = a, a = b, b = tmp; } /*ユークリッドの互除法*/ r = a % b; while (r != 0) { a = b, b = r, r = a % b; } return b; } int LCM(int a, int b) { int c = a, d = b, r, tmp; /*自然数 a > b を確認・入替*/ if (a < b) { tmp = a, a = b, b = tmp; } /*ユークリッドの互除法*/ r = a % b; while (r != 0) { a = b, b = r, r = a % b; } return c / b * d; } int Factorial(int n) { int m = 1; while (n >= 1) m *= n, n--; return m; } /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ int main() { long long int n, k; cin >> k >> n; if (n == 1) { cout << 0 << endl; return 0; } vector<long long int> v(n), w(n - 1); rep(i, n) cin >> v[i]; sort(v.begin(), v.end()); rep(i, n - 1) { w[i] = v[i + 1] - v[i]; } sort(w.begin(), w.end(), greater<int>()); long long int ans = Svec(w); if (k == 1) { cout << ans << endl; return 0; } rep(i, k - 1) { ans -= w[i]; } cout << ans << endl; }
/*Function Template*/ #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; #define rep(i, n) for (long long int i = 0; i < (n); i++) int Len(int n) { int s = 0; while (n != 0) s++, n /= 10; return s; } int Sint(int n) { int m = 0, s = 0, a = n; while (a != 0) s++, a /= 10; for (int i = s - 1; i >= 0; i--) m += n / ((int)pow(10, i)) - (n / ((int)pow(10, i + 1))) * 10; return m; } long long int Svec(vector<long long int> v) { long long int n = 0; for (long long int i = 0; i < v.size(); i++) n += v[i]; return n; } int GCD(int a, int b) { int r, tmp; /*自然数 a > b を確認・入替*/ if (a < b) { tmp = a, a = b, b = tmp; } /*ユークリッドの互除法*/ r = a % b; while (r != 0) { a = b, b = r, r = a % b; } return b; } int LCM(int a, int b) { int c = a, d = b, r, tmp; /*自然数 a > b を確認・入替*/ if (a < b) { tmp = a, a = b, b = tmp; } /*ユークリッドの互除法*/ r = a % b; while (r != 0) { a = b, b = r, r = a % b; } return c / b * d; } int Factorial(int n) { int m = 1; while (n >= 1) m *= n, n--; return m; } /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ int main() { long long int n, k; cin >> k >> n; if (n == 1) { cout << 0 << endl; return 0; } vector<long long int> v(n), w(n - 1); rep(i, n) cin >> v[i]; sort(v.begin(), v.end()); rep(i, n - 1) { w[i] = v[i + 1] - v[i]; } sort(w.begin(), w.end(), greater<int>()); long long int ans = Svec(w); if (k == 1) { cout << ans << endl; return 0; } if (k - 1 >= w.size()) { cout << 0 << endl; return 0; } rep(i, k - 1) { ans -= w[i]; } cout << ans << endl; }
insert
83
83
83
87
0
p03137
C++
Runtime Error
#include <iostream> #include <queue> using namespace std; int main() { int n, m, k; cin >> n >> m; priority_queue<int> a; for (int i = 0; i < m; i++) { cin >> k; a.push(k); } priority_queue<int> b; int dum; while (-1) { dum = a.top(); a.pop(); if (a.empty()) { break; } b.push(dum - a.top()); } while (n > 1) { b.pop(); n--; } int result = 0; while (-1) { if (b.empty()) { break; } result += b.top(); b.pop(); } cout << result << endl; }
#include <iostream> #include <queue> using namespace std; int main() { int n, m, k; cin >> n >> m; priority_queue<int> a; for (int i = 0; i < m; i++) { cin >> k; a.push(k); } priority_queue<int> b; int dum; while (-1) { dum = a.top(); a.pop(); if (a.empty()) { break; } b.push(dum - a.top()); } while (n > 1 && !b.empty()) { b.pop(); n--; } int result = 0; while (-1) { if (b.empty()) { break; } result += b.top(); b.pop(); } cout << result << endl; }
replace
28
29
28
29
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (m == 1 || m >= n) { cout << "0" << endl; return 0; } vector<int> x(m); vector<pair<int, int>> dist; for (int i = 0; i < m; i++) { cin >> x.at(i); x.at(i) += 100000; } sort(x.begin(), x.end()); for (int i = 1; i < m; i++) { int dif = x.at(i) - x.at(i - 1); dist.push_back(make_pair(dif, i)); } sort(dist.begin(), dist.end()); reverse(dist.begin(), dist.end()); vector<int> koma(n); koma[0] = -1; for (int i = 0; i < n - 1; i++) { koma[i + 1] = dist[i].second - 1; } sort(koma.begin(), koma.end()); int ans = 0; for (int i = 1; i < n; i++) { ans += x[koma[i]] - x[koma[i - 1] + 1]; } ans += x[m - 1] - x[koma[n - 1] + 1]; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (m == 1 || n >= m) { cout << "0" << endl; return 0; } vector<int> x(m); vector<pair<int, int>> dist; for (int i = 0; i < m; i++) { cin >> x.at(i); x.at(i) += 100000; } sort(x.begin(), x.end()); for (int i = 1; i < m; i++) { int dif = x.at(i) - x.at(i - 1); dist.push_back(make_pair(dif, i)); } sort(dist.begin(), dist.end()); reverse(dist.begin(), dist.end()); vector<int> koma(n); koma[0] = -1; for (int i = 0; i < n - 1; i++) { koma[i + 1] = dist[i].second - 1; } sort(koma.begin(), koma.end()); int ans = 0; for (int i = 1; i < n; i++) { ans += x[koma[i]] - x[koma[i - 1] + 1]; } ans += x[m - 1] - x[koma[n - 1] + 1]; cout << ans << endl; }
replace
6
7
6
7
0
p03137
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; int sum = 0; int x[m]; for (int i = 0; i < m; ++i) cin >> x[i]; sort(x, x + m); // m - 1個の区間を選ばなくていい for (int i = 0; i < m - 1; ++i) x[i] = x[i + 1] - x[i]; x[m - 1] = 0; for (int i = 0; i < m; ++i) sum += x[i]; sort(x, x + m, greater<int>()); int notchoose = 0; for (int i = 0; i < n - 1; ++i) notchoose += x[i]; cout << sum - notchoose << endl; }
#include <algorithm> #include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; if (n >= m) { cout << 0 << endl; return 0; } int sum = 0; int x[m]; for (int i = 0; i < m; ++i) cin >> x[i]; sort(x, x + m); // m - 1個の区間を選ばなくていい for (int i = 0; i < m - 1; ++i) x[i] = x[i + 1] - x[i]; x[m - 1] = 0; for (int i = 0; i < m; ++i) sum += x[i]; sort(x, x + m, greater<int>()); int notchoose = 0; for (int i = 0; i < n - 1; ++i) notchoose += x[i]; cout << sum - notchoose << endl; }
insert
9
9
9
13
0
p03137
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct distandindex { int dist; int index; }; bool cmp(const distandindex &a, const distandindex &b) { return a.dist > b.dist; } int main() { int N, M; cin >> N >> M; vector<int> x(M); for (int i = 0; i < M; i++) { cin >> x[i]; } std::sort(x.begin(), x.end()); if (M == 1) { cout << 0 << endl; return 0; } if (N == 1) { cout << x[M - 1] - x[0] << endl; return 0; } vector<distandindex> kaisa(M - 1); for (int i = 0; i < M - 1; i++) { distandindex d = {x[i + 1] - x[i], i}; kaisa[i] = d; } std::sort(kaisa.begin(), kaisa.end(), cmp); vector<int> index(N - 1); for (int i = 0; i < N - 1; i++) { index[i] = kaisa[i].index; } int start = 0; int sum = 0; for (int i = 0; i < N - 1; i++) { sum += x[index[i]] - x[start]; start = index[i] + 1; } sum += x[M - 1] - x[start]; cout << sum << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct distandindex { int dist; int index; }; bool cmp(const distandindex &a, const distandindex &b) { return a.dist > b.dist; } int main() { int N, M; cin >> N >> M; vector<int> x(M); for (int i = 0; i < M; i++) { cin >> x[i]; } std::sort(x.begin(), x.end()); if (M == 1 || N >= M) { cout << 0 << endl; return 0; } if (N == 1) { cout << x[M - 1] - x[0] << endl; return 0; } vector<distandindex> kaisa(M - 1); for (int i = 0; i < M - 1; i++) { distandindex d = {x[i + 1] - x[i], i}; kaisa[i] = d; } std::sort(kaisa.begin(), kaisa.end(), cmp); vector<int> index(N - 1); for (int i = 0; i < N - 1; i++) { index[i] = kaisa[i].index; } int start = 0; int sum = 0; for (int i = 0; i < N - 1; i++) { sum += x[index[i]] - x[start]; start = index[i] + 1; } sum += x[M - 1] - x[start]; cout << sum << endl; return 0; }
replace
24
25
24
25
0
p03137
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int INF = 1e9; const int MAX = 100000; const long long LINF = 1e18; int main() { int N, M; cin >> N >> M; vector<int> X(M); for (int i = 0; i < M; i++) { cin >> X[i]; } sort(X.begin(), X.end()); int L = X[M - 1] - X[0]; int ans = 0; if (M > 1) { if (N > 1) { vector<int> l(M - 1); for (int i = 0; i < M - 1; i++) { l[i] = X[i + 1] - X[i]; } sort(l.begin(), l.end(), greater<int>()); int sum = 0; for (int i = 0; i < N - 1; i++) { sum += l[i]; } ans = L - sum; } else ans = L; } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int INF = 1e9; const int MAX = 100000; const long long LINF = 1e18; int main() { int N, M; cin >> N >> M; vector<int> X(M); for (int i = 0; i < M; i++) { cin >> X[i]; } sort(X.begin(), X.end()); int L = X[M - 1] - X[0]; int ans = 0; if (M > 1 && M >= N) { if (N > 1) { vector<int> l(M - 1); for (int i = 0; i < M - 1; i++) { l[i] = X[i + 1] - X[i]; } sort(l.begin(), l.end(), greater<int>()); int sum = 0; for (int i = 0; i < N - 1; i++) { sum += l[i]; } ans = L - sum; } else ans = L; } cout << ans << endl; }
replace
19
20
19
20
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(a, b) for (int a = 0; a < (b); ++a) #define REP1(i, n) for (int i = 1; i <= (n); ++i) #define debug(x) cerr << #x << ": " << x << '\n' #define all(x) (x).begin(), (x).end() #define YES() printf("YES\n") #define NO() printf("NO\n") #define isYES(x) printf("%s\n", (x) ? "YES" : "NO") #define Yes() printf("Yes\n") #define No() printf("No\n") #define isYes(x) printf("%s\n", (x) ? "Yes" : "No") #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair #define INF (1 << 29) #define Sp(p) cout << setprecision(25) << fixed << p << endl #define vi vector<int> #define vl vector<ll> #define vii vector<vector<int>> #define vll vector<vector<ll>> #define vs vector<string> #define pii pair<int, int> #define pis pair<int, string> #define psi pair<string, int> #define pll pair<ll, ll> #define X first #define Y second using namespace std; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> PP; typedef pair<ll, LP> LPP; template <class T = int> T in() { T x; cin >> x; return (x); } template <class T> void print(T &x) { cout << x << '\n'; } const int MOD = (int)1e9 + 7; const int MAX = 510000; ll fac[MAX], finv[MAX], inv[MAX]; void COMint() { 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; } } ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll a, ll b) { if (b == 0) return a; if (a > b) { swap(a, b); } return gcd(a, b % a); } ll lcm(ll a, ll b) { ll g; g = gcd(a, b); return a * b / g; } bool compare_by_b(pair<int, int> a, pair<int, int> b) { if (a.second != b.second) { return a.second < b.second; } else { return a.first < b.first; } } bool compare_by_a(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) { return a.first < b.first; } else { return a.second < b.second; } } 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; } ll RS(ll N, ll P, ll M) { if (P == 0) { return 1; } else { if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } else { return N * RS(N, P - 1, M) % M; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int N = in(); int M = in(); vector<int> vec(M); REP(i, M) { cin >> vec[i]; } if (M == 1) { cout << 0 << endl; return 0; } sort(all(vec)); ll ans = 0; ans = vec[vec.size() - 1] - vec[0]; vector<int> aida(M - 1); int now; now = vec[0]; REP(i, M - 1) { aida[i] = vec[i + 1] - now; now = vec[i + 1]; } sort(all(aida)); REP(i, N - 1) { ans -= aida[aida.size() - 1 - i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(a, b) for (int a = 0; a < (b); ++a) #define REP1(i, n) for (int i = 1; i <= (n); ++i) #define debug(x) cerr << #x << ": " << x << '\n' #define all(x) (x).begin(), (x).end() #define YES() printf("YES\n") #define NO() printf("NO\n") #define isYES(x) printf("%s\n", (x) ? "YES" : "NO") #define Yes() printf("Yes\n") #define No() printf("No\n") #define isYes(x) printf("%s\n", (x) ? "Yes" : "No") #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair #define INF (1 << 29) #define Sp(p) cout << setprecision(25) << fixed << p << endl #define vi vector<int> #define vl vector<ll> #define vii vector<vector<int>> #define vll vector<vector<ll>> #define vs vector<string> #define pii pair<int, int> #define pis pair<int, string> #define psi pair<string, int> #define pll pair<ll, ll> #define X first #define Y second using namespace std; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> PP; typedef pair<ll, LP> LPP; template <class T = int> T in() { T x; cin >> x; return (x); } template <class T> void print(T &x) { cout << x << '\n'; } const int MOD = (int)1e9 + 7; const int MAX = 510000; ll fac[MAX], finv[MAX], inv[MAX]; void COMint() { 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; } } ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll a, ll b) { if (b == 0) return a; if (a > b) { swap(a, b); } return gcd(a, b % a); } ll lcm(ll a, ll b) { ll g; g = gcd(a, b); return a * b / g; } bool compare_by_b(pair<int, int> a, pair<int, int> b) { if (a.second != b.second) { return a.second < b.second; } else { return a.first < b.first; } } bool compare_by_a(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) { return a.first < b.first; } else { return a.second < b.second; } } 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; } ll RS(ll N, ll P, ll M) { if (P == 0) { return 1; } else { if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } else { return N * RS(N, P - 1, M) % M; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int N = in(); int M = in(); vector<int> vec(M); REP(i, M) { cin >> vec[i]; } if (M == 1 || N >= M) { cout << 0 << endl; return 0; } sort(all(vec)); ll ans = 0; ans = vec[vec.size() - 1] - vec[0]; vector<int> aida(M - 1); int now; now = vec[0]; REP(i, M - 1) { aida[i] = vec[i + 1] - now; now = vec[i + 1]; } sort(all(aida)); REP(i, N - 1) { ans -= aida[aida.size() - 1 - i]; } cout << ans << endl; return 0; }
replace
127
128
127
128
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> X(M); for (int i = 0; i < M; ++i) { cin >> X.at(i); } sort(X.begin(), X.end()); if (M == 1) { cout << 0 << endl; } else { vector<int> distance(M - 1); int X_width = X.at(M - 1) - X.at(0); for (int i = 0; i < M - 1; ++i) { distance.at(i) = X.at(i + 1) - X.at(i); } sort(distance.begin(), distance.end(), greater<int>()); for (int i = 0; i < min(N - 1, M); ++i) { X_width = X_width - distance.at(i); } cout << X_width << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> X(M); for (int i = 0; i < M; ++i) { cin >> X.at(i); } sort(X.begin(), X.end()); if (M == 1) { cout << 0 << endl; } else { vector<int> distance(M - 1); int X_width = X.at(M - 1) - X.at(0); for (int i = 0; i < M - 1; ++i) { distance.at(i) = X.at(i + 1) - X.at(i); } sort(distance.begin(), distance.end(), greater<int>()); for (int i = 0; i < min(N - 1, M - 1); ++i) { X_width = X_width - distance.at(i); } cout << X_width << endl; } }
replace
23
24
23
24
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define COUT(x) cout << (x) << endl #define dump(x) cout << #x << " = " << (x) << endl; #define Yes(x) cout << (x ? "Yes" : "No") << endl; #define YES(x) cout << (x ? "YES" : "NO") << endl; using ll = long long; using P = pair<int, int>; using Graph = vector<vector<int>>; using M = map<int, int>; using PQ = priority_queue<int>; using PQG = priority_queue<int, vector<int>, greater<int>>; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { int n, m; cin >> n >> m; int x[m]; REP(i, m) cin >> x[i]; sort(x, x + m); int ans = 0; vector<int> d(m + 1, 0); for (int i = 1; i < m; i++) { d[i] = abs(x[i] - x[i - 1]); ans += d[i]; } sort(d.begin(), d.end(), greater<int>()); REP(i, max(n - 1, m)) ans -= d[i]; COUT(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define COUT(x) cout << (x) << endl #define dump(x) cout << #x << " = " << (x) << endl; #define Yes(x) cout << (x ? "Yes" : "No") << endl; #define YES(x) cout << (x ? "YES" : "NO") << endl; using ll = long long; using P = pair<int, int>; using Graph = vector<vector<int>>; using M = map<int, int>; using PQ = priority_queue<int>; using PQG = priority_queue<int, vector<int>, greater<int>>; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { int n, m; cin >> n >> m; int x[m]; REP(i, m) cin >> x[i]; sort(x, x + m); int ans = 0; vector<int> d(m + 1, 0); for (int i = 1; i < m; i++) { d[i] = abs(x[i] - x[i - 1]); ans += d[i]; } sort(d.begin(), d.end(), greater<int>()); REP(i, min(n - 1, m)) ans -= d[i]; COUT(ans); return 0; }
replace
33
34
33
34
0
p03137
C++
Runtime Error
#define _GLIBCXX_DEBUG // TLEの原因になるので注意!!!!!!!!!!! #include <bits/stdc++.h> #include <cmath> typedef long long ll; using namespace std; vector<int> arr; stack<int> st; queue<int> qu; queue<pair<int, int>> qu2; priority_queue<int> pq; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, n) for (int i = 1; i <= (int)(n); i++) typedef set<int> set_t; typedef set<string> set_g; typedef complex<double> xy_t; static const int NIL = -1; static const int INF = 1000000007; #define mp make_pair /*int P[10010]; void init(int n){//Union-find for(int i=0;i<n;++i)P[i]=i; } int root(int a) { if(P[a]==a)return a; return (P[a]=root(P[a])); } bool is_same_set(int a,int b){ return root(a) == root(b);//代表元を求める } void unite(int a,int b){ P[root(a)]=root(b); }//対set,グラフ? //セグメント木?? */ // Unionfind ,Disjointset class UnionFind { public: // 親の番号を格納する 親の場合-(その集合のサイズ) vector<int> Parent; // 作るときはParentの値を全て-1にする UnionFind(int n) { Parent = vector<int>(n, -1); } // Aがどのグループに属するか int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } // 自分のグループの頂点数 int size(int A) { return -Parent[root(A)]; } // AとBをくっつける int connect(int A, int B) { // AとBを直接ではなくAのrootとBのrootをくっつける A = root(A); B = root(B); if (A == B) { return false; // すでにくっついている } if (size(A) < size(B)) swap(A, B); // Aのサイズ更新 Parent[A] += Parent[B]; // Bの親をAに変更する Parent[B] = A; return true; } }; double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); } // 内積 double cross_product(xy_t a, xy_t b) { return (conj(a) * b).imag(); } // 外積 xy_t projection(xy_t p, xy_t b) { return b * dot_product(p, b) / norm(b); } // 投影 // 対図形 #define mod 1000000007 /*ll f[2001]; //int n,k とかしておく ll pw(ll x, ll y){//euclidの互除法より ll a= 1; while(y){ if(y&1){//奇数なら a = a*x%mod; } x = x*x%mod; y /= 2; } return a; } ll modinv(ll x){//逆元を求める return pw(x, mod - 2 ); } ll comb(int n,int r){ if(n<r){ return 0; } return f[n] * modinv(f[r])%mod*modinv(f[n-r])%mod; }//対combination//ただしfは用意してね */ struct BIT { vector<int> dat; int sz; BIT(int n) { for (sz = 1; sz < n; sz *= 2) ; dat.resize(++sz); } int q(int n) { int ret = 0; for (int i = n; i > 0; i -= i & -i) ret += dat[i]; // 和の計算 iから最後の1のbit(i&-i // 多分&はビット積)を減算 for (int i = n; i < sz; i += i & -i) dat[i]++; // 値の加算 iから最後mの1のbitを加算 return ret; } }; /* // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this;//thisポインター 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*(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, m; cin >> n >> m; vector<int> x(m); rep(i, m) { cin >> x[i]; } if (m == 1) { cout << 0 << endl; return 0; } sort(x.begin(), x.end()); int temp = 0; vector<int> y(m - 1); rep(i, m - 1) { y[i] = x[i + 1] - x[i]; } sort(y.begin(), y.end(), greater<int>()); rep(i, n - 1) { temp += y[i]; } cout << x[m - 1] - x[0] - temp << endl; return 0; }
#define _GLIBCXX_DEBUG // TLEの原因になるので注意!!!!!!!!!!! #include <bits/stdc++.h> #include <cmath> typedef long long ll; using namespace std; vector<int> arr; stack<int> st; queue<int> qu; queue<pair<int, int>> qu2; priority_queue<int> pq; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, n) for (int i = 1; i <= (int)(n); i++) typedef set<int> set_t; typedef set<string> set_g; typedef complex<double> xy_t; static const int NIL = -1; static const int INF = 1000000007; #define mp make_pair /*int P[10010]; void init(int n){//Union-find for(int i=0;i<n;++i)P[i]=i; } int root(int a) { if(P[a]==a)return a; return (P[a]=root(P[a])); } bool is_same_set(int a,int b){ return root(a) == root(b);//代表元を求める } void unite(int a,int b){ P[root(a)]=root(b); }//対set,グラフ? //セグメント木?? */ // Unionfind ,Disjointset class UnionFind { public: // 親の番号を格納する 親の場合-(その集合のサイズ) vector<int> Parent; // 作るときはParentの値を全て-1にする UnionFind(int n) { Parent = vector<int>(n, -1); } // Aがどのグループに属するか int root(int A) { if (Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } // 自分のグループの頂点数 int size(int A) { return -Parent[root(A)]; } // AとBをくっつける int connect(int A, int B) { // AとBを直接ではなくAのrootとBのrootをくっつける A = root(A); B = root(B); if (A == B) { return false; // すでにくっついている } if (size(A) < size(B)) swap(A, B); // Aのサイズ更新 Parent[A] += Parent[B]; // Bの親をAに変更する Parent[B] = A; return true; } }; double dot_product(xy_t a, xy_t b) { return (conj(a) * b).real(); } // 内積 double cross_product(xy_t a, xy_t b) { return (conj(a) * b).imag(); } // 外積 xy_t projection(xy_t p, xy_t b) { return b * dot_product(p, b) / norm(b); } // 投影 // 対図形 #define mod 1000000007 /*ll f[2001]; //int n,k とかしておく ll pw(ll x, ll y){//euclidの互除法より ll a= 1; while(y){ if(y&1){//奇数なら a = a*x%mod; } x = x*x%mod; y /= 2; } return a; } ll modinv(ll x){//逆元を求める return pw(x, mod - 2 ); } ll comb(int n,int r){ if(n<r){ return 0; } return f[n] * modinv(f[r])%mod*modinv(f[n-r])%mod; }//対combination//ただしfは用意してね */ struct BIT { vector<int> dat; int sz; BIT(int n) { for (sz = 1; sz < n; sz *= 2) ; dat.resize(++sz); } int q(int n) { int ret = 0; for (int i = n; i > 0; i -= i & -i) ret += dat[i]; // 和の計算 iから最後の1のbit(i&-i // 多分&はビット積)を減算 for (int i = n; i < sz; i += i & -i) dat[i]++; // 値の加算 iから最後mの1のbitを加算 return ret; } }; /* // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this;//thisポインター 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*(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, m; cin >> n >> m; vector<int> x(m); rep(i, m) { cin >> x[i]; } if (m < n) { cout << 0 << endl; return 0; } sort(x.begin(), x.end()); int temp = 0; vector<int> y(m - 1); rep(i, m - 1) { y[i] = x[i + 1] - x[i]; } sort(y.begin(), y.end(), greater<int>()); rep(i, n - 1) { temp += y[i]; } cout << x[m - 1] - x[0] - temp << endl; return 0; }
replace
184
185
184
185
0
p03137
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #define ll long long #define ld long double using namespace std; int read() { int x; scanf("%d", &x); return x; } int maxn, minn; const int MAXN = 100005; int a[MAXN], b[MAXN]; int main() { int n = read(), m = read(); minn = 1e9 + 7, maxn = -1e9; for (int i = 1; i <= m; i++) a[i] = read(), minn = min(minn, a[i]), maxn = max(maxn, a[i]); sort(a + 1, a + m + 1); for (int i = 1; i < m; i++) b[i] = a[i + 1] - a[i]; sort(b + 1, b + m); int ans = maxn - minn; for (int i = 1; i < n; i++) ans -= b[m - i]; printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #define ll long long #define ld long double using namespace std; int read() { int x; scanf("%d", &x); return x; } int maxn, minn; const int MAXN = 100005; int a[MAXN], b[MAXN]; int main() { int n = read(), m = read(); if (n > m) n = m; minn = 1e9 + 7, maxn = -1e9; for (int i = 1; i <= m; i++) a[i] = read(), minn = min(minn, a[i]), maxn = max(maxn, a[i]); sort(a + 1, a + m + 1); for (int i = 1; i < m; i++) b[i] = a[i + 1] - a[i]; sort(b + 1, b + m); int ans = maxn - minn; for (int i = 1; i < n; i++) ans -= b[m - i]; printf("%d\n", ans); return 0; }
insert
18
18
18
20
0
p03137
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <vector> using namespace std; #pragma warning(disable : 4996) int main() { int n, m; cin >> n >> m; vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i]; sort(a.begin(), a.end()); vector<int> vc; for (int i = 1; i < m; i++) vc.push_back(abs(a[i] - a[i - 1])); sort(vc.begin(), vc.end()); long long ans = 0; for (int i = 0; i < abs(n - m); i++) ans += 1LL * vc[i]; cout << ans; }
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <vector> using namespace std; #pragma warning(disable : 4996) int main() { int n, m; cin >> n >> m; vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i]; if (n >= m) { cout << 0; return 0; } sort(a.begin(), a.end()); vector<int> vc; for (int i = 1; i < m; i++) vc.push_back(abs(a[i] - a[i - 1])); sort(vc.begin(), vc.end()); long long ans = 0; for (int i = 0; i < abs(n - m); i++) ans += 1LL * vc[i]; cout << ans; }
insert
14
14
14
18
0
p03137
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdlib> #include <cstring> #include <exception> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <vector> using namespace std; typedef long long ll; typedef string str; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<char> vc; typedef vector<str> vs; typedef vector<P> vp; typedef vector<bool> vb; #define rep(i, a, n) for (ll i = (a); i < (n); ++i) #define rrep(i, a, n) for (ll i = (a); i > (n); --i) #define erep(i, a, n) for (ll i = (a); i <= (n); ++i) #define rerep(i, a, n) for (ll i = (a); i >= (n); --i) #define all(c) (c).begin(), (c).end() #define pb push_back #define mp make_pair #define sz(x) ((int)x.size()) #define vec_max(V) *max_element(all(V)) #define vec_min(V) *min_element(all(V)) #define vec_sum(V) accumulate(all(V), 0LL) #define print(X) cout << (X) << "\n" #define _ << " " << #define Yes print("Yes") #define No print("No") static const int INF = 1e+9 + 7; int main() { ll N, M; cin >> N >> M; vll X(M); rep(i, 0, M) cin >> X[i]; if (M == 1) print(0); else { sort(all(X)); vll sa(M - 1); rep(i, 0, M - 1) { sa[i] = abs(X[i + 1] - X[i]); } sort(all(sa)); ll sum_sa = vec_sum(sa); rep(i, 0, N - 1) sum_sa -= sa[M - 2 - i]; print(sum_sa); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdlib> #include <cstring> #include <exception> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <vector> using namespace std; typedef long long ll; typedef string str; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<char> vc; typedef vector<str> vs; typedef vector<P> vp; typedef vector<bool> vb; #define rep(i, a, n) for (ll i = (a); i < (n); ++i) #define rrep(i, a, n) for (ll i = (a); i > (n); --i) #define erep(i, a, n) for (ll i = (a); i <= (n); ++i) #define rerep(i, a, n) for (ll i = (a); i >= (n); --i) #define all(c) (c).begin(), (c).end() #define pb push_back #define mp make_pair #define sz(x) ((int)x.size()) #define vec_max(V) *max_element(all(V)) #define vec_min(V) *min_element(all(V)) #define vec_sum(V) accumulate(all(V), 0LL) #define print(X) cout << (X) << "\n" #define _ << " " << #define Yes print("Yes") #define No print("No") static const int INF = 1e+9 + 7; int main() { ll N, M; cin >> N >> M; vll X(M); rep(i, 0, M) cin >> X[i]; if (N >= M) print(0); else { sort(all(X)); vll sa(M - 1); rep(i, 0, M - 1) { sa[i] = abs(X[i + 1] - X[i]); } sort(all(sa)); ll sum_sa = vec_sum(sa); rep(i, 0, N - 1) sum_sa -= sa[M - 2 - i]; print(sum_sa); } return 0; }
replace
49
50
49
50
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (long i = 0; i < n; ++i) using namespace std; void solve(long N, long M, vector<long> X) { // if there's only one target, we can win without any move if (M == 1) { cout << 0 << endl; return; } // sort the targets sort(X.begin(), X.end()); // calc the distances between targets vector<long> dist(M - 1); REP(i, M - 1) dist[i] = X[i + 1] - X[i]; // sort the distances sort(dist.rbegin(), dist.rend()); // if we have no extra piece, we should move the first piece from the begin to // the end long ans(X.back() - X.front()); // we can skip the N-1 longest gap by putting pieces in advance REP(i, N - 1) ans -= dist[i]; cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); long N, M; cin >> N >> M; vector<long> X(M); REP(i, M) cin >> X[i]; solve(N, M, move(X)); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (long i = 0; i < n; ++i) using namespace std; void solve(long N, long M, vector<long> X) { // if there's less number of targets than the pieces, we can win without any // movement if (M <= N) { cout << 0 << endl; return; } // sort the targets sort(X.begin(), X.end()); // calc the distances between targets vector<long> dist(M - 1); REP(i, M - 1) dist[i] = X[i + 1] - X[i]; // sort the distances sort(dist.rbegin(), dist.rend()); // if we have no extra piece, we should move the first piece from the begin to // the end long ans(X.back() - X.front()); // we can skip the N-1 longest gap by putting pieces in advance REP(i, N - 1) ans -= dist[i]; cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); long N, M; cin >> N >> M; vector<long> X(M); REP(i, M) cin >> X[i]; solve(N, M, move(X)); return 0; }
replace
6
8
6
9
0
p03137
C++
Runtime Error
/*{{{*/ #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define CASET \ int ___T; \ scanf("%d", &___T); \ for (int cs = 1; cs <= ___T; cs++) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL, LL> PLL; typedef vector<PLL> VPLL; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(LL &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const LL &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.F); putchar(' '); _W(x.S); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } #ifdef HOME #define DEBUG(...) \ { \ printf("# "); \ printf(__VA_ARGS__); \ puts(""); \ } #else #define DEBUG(...) #endif int MOD = 1e9 + 7; void ADD(LL &x, LL v) { x = (x + v) % MOD; if (x < 0) x += MOD; } /*}}}*/ const int SIZE = 1e6 + 10; int main() { int N, M; R(N, M); VI AA(M); REP(i, M) { R(AA[i]); } sort(ALL(AA)); VI d; REPP(i, 1, SZ(AA)) { d.PB(AA[i] - AA[i - 1]); } LL an = AA.back() - AA[0]; sort(ALL(d), greater<int>()); REP(i, N - 1) an -= d[i]; W(an); return 0; }
/*{{{*/ #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <limits.h> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) \ (sort(c.begin(), c.end()), \ c.resize(distance(c.begin(), unique(c.begin(), c.end())))) #define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin()) #define CASET \ int ___T; \ scanf("%d", &___T); \ for (int cs = 1; cs <= ___T; cs++) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL, LL> PLL; typedef vector<PLL> VPLL; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(LL &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const LL &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.F); putchar(' '); _W(x.S); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } #ifdef HOME #define DEBUG(...) \ { \ printf("# "); \ printf(__VA_ARGS__); \ puts(""); \ } #else #define DEBUG(...) #endif int MOD = 1e9 + 7; void ADD(LL &x, LL v) { x = (x + v) % MOD; if (x < 0) x += MOD; } /*}}}*/ const int SIZE = 1e6 + 10; int main() { int N, M; R(N, M); if (N >= M) { W(0); return 0; } VI AA(M); REP(i, M) { R(AA[i]); } sort(ALL(AA)); VI d; REPP(i, 1, SZ(AA)) { d.PB(AA[i] - AA[i - 1]); } LL an = AA.back() - AA[0]; sort(ALL(d), greater<int>()); REP(i, N - 1) an -= d[i]; W(an); return 0; }
insert
102
102
102
106
0
p03137
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; #define ll long long #define pb push_back int main() { int N, M; vector<int> X; cin >> N >> M; for (int i = 0; i < M; i++) { int x; cin >> x; X.pb(x); } sort(X.begin(), X.end()); ll ans = X[X.size() - 1] - X[0]; vector<int> dif; for (int i = 1; i < M; i++) { dif.pb(X[i] - X[i - 1]); } sort(dif.begin(), dif.end()); for (int i = 0; i < N - 1; i++) { ans -= dif[dif.size() - 1 - i]; } cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; #define ll long long #define pb push_back int main() { int N, M; vector<int> X; cin >> N >> M; if (N >= M) { cout << 0 << endl; return 0; } for (int i = 0; i < M; i++) { int x; cin >> x; X.pb(x); } sort(X.begin(), X.end()); ll ans = X[X.size() - 1] - X[0]; vector<int> dif; for (int i = 1; i < M; i++) { dif.pb(X[i] - X[i - 1]); } sort(dif.begin(), dif.end()); for (int i = 0; i < N - 1; i++) { ans -= dif[dif.size() - 1 - i]; } cout << ans << endl; return 0; }
insert
18
18
18
22
0
p03137
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define endl "\n" #define int long long typedef vector<int> vi; #define all(x) x.begin(), x.end() #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) #define OO 1e9 #define DEBUG(x) cout << '>' << #x << ':' << x << endl; typedef long long ll; #define in_range(x, y, r, c) ((x >= 0 && x < r && y >= 0 && y < c)) const double eps = 1e-9; const int MOD = 1e10; const int N = 1e5 + 9; const int inf = 1LL << 32; template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } template <typename T> T exp(T b, T p) { T x = 1; while (p) { if (p & 1) x = (x * b); b = (b * b); p = p >> 1; } return x; } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } int32_t main() { IOS; int n, m; cin >> n >> m; vi arr(m); for (int i = 0; i < m; ++i) { cin >> arr[i]; } vi ans; sort(all(arr)); for (int i = 0; i < m - 1; ++i) { ans.emplace_back(abs(arr[i] - arr[i + 1])); } sort(ans.rbegin(), ans.rend()); int sum = 0, idx = (m - n); while (idx--) sum += ans.back(), ans.pop_back(); cout << sum; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define endl "\n" #define int long long typedef vector<int> vi; #define all(x) x.begin(), x.end() #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (a); i >= (b); i--) #define OO 1e9 #define DEBUG(x) cout << '>' << #x << ':' << x << endl; typedef long long ll; #define in_range(x, y, r, c) ((x >= 0 && x < r && y >= 0 && y < c)) const double eps = 1e-9; const int MOD = 1e10; const int N = 1e5 + 9; const int inf = 1LL << 32; template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } template <typename T> T exp(T b, T p) { T x = 1; while (p) { if (p & 1) x = (x * b); b = (b * b); p = p >> 1; } return x; } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } int32_t main() { IOS; int n, m; cin >> n >> m; vi arr(m); for (int i = 0; i < m; ++i) { cin >> arr[i]; } vi ans; sort(all(arr)); for (int i = 0; i < m - 1; ++i) { ans.emplace_back(abs(arr[i] - arr[i + 1])); } sort(ans.rbegin(), ans.rend()); int sum = 0; while (n < m) { sum += ans.back(); ans.pop_back(); n++; } cout << sum; }
replace
56
59
56
62
0
p03139
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0, a + b - n); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { /* #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif*/ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0, a + b - n); }
replace
5
9
5
10
0
p03139
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse2") #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define FF first #define SS second #define sz(x) (int)x.size() #define endl '\n' #define hell 1000000007 #define rep(i, a, b) for (int i = a; i < b; i++) int solve() { int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0, a + b - n); return 0; } int main() { int t = 1; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif clock_t time_req; // Without using pow function time_req = clock(); // cin>>t; bool flag = true; while (t--) { solve(); } time_req = clock() - time_req; // cout << "Processor time taken is " // << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse2") #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define FF first #define SS second #define sz(x) (int)x.size() #define endl '\n' #define hell 1000000007 #define rep(i, a, b) for (int i = a; i < b; i++) int solve() { int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0, a + b - n); return 0; } int main() { int t = 1; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t time_req; // Without using pow function time_req = clock(); // cin>>t; bool flag = true; while (t--) { solve(); } time_req = clock() - time_req; // cout << "Processor time taken is " // << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl; return 0; }
delete
38
43
38
38
0
p03139
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define sd(x) scanf("%d", &x) #define slld(x) scanf("%lld", &x) #define all(x) x.begin(), x.end() #define For(i, st, en) for (ll i = st; i < en; i++) #define tr(x) for (auto it = x.begin(); it != x.end(); it++) #define fast \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); #define pb push_back #define ll long long #define int long long #define mp make_pair #define F first #define S second typedef pair<int, int> pii; #define MOD 1000000007 signed main() { fast; #ifndef ONLINE_JUDGE freopen("/home/swapnil/Desktop/c++/input.txt", "r", stdin); freopen("/home/swapnil/Desktop/c++/output.txt", "w", stdout); #endif int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0LL, a + b - n); return 0; }
#include <bits/stdc++.h> using namespace std; #define sd(x) scanf("%d", &x) #define slld(x) scanf("%lld", &x) #define all(x) x.begin(), x.end() #define For(i, st, en) for (ll i = st; i < en; i++) #define tr(x) for (auto it = x.begin(); it != x.end(); it++) #define fast \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); #define pb push_back #define ll long long #define int long long #define mp make_pair #define F first #define S second typedef pair<int, int> pii; #define MOD 1000000007 signed main() { fast; // #ifndef ONLINE_JUDGE // freopen("/home/swapnil/Desktop/c++/input.txt","r",stdin); // freopen("/home/swapnil/Desktop/c++/output.txt","w",stdout); // #endif int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0LL, a + b - n); return 0; }
replace
21
25
21
25
0
p03139
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define Abs(x) ((x) < 0 ? (x) * -1 : (x)) #define rep(x, y) for ((x) = 0; (x) < (y); (x)++) #define repin(x, y) for ((x) = 0; (x) <= (y); (x)++) #define nep(x, y) for ((x) = (y)-1; 0 <= (x); (x)--) #define nepi(x, y, z) for ((x) = (y)-1; (z) <= (x); (x)--) #define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++) #define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++) #define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z)) #define repis(x, y, z, s) for ((x) = (z); (x) < (y); (x) += (s)) #define repiins(x, y, z, s) for ((x) = (z); (x) <= (y); (x) += (s)) #define repit(x) \ for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++) #define nepit(x) \ for (__typeof((x).rbegin()) it = (x).rbegin(); it != (x).rend(); it++) #define All(x) (x).begin(), (x).end() #define Mem0(x) memset(x, 0, sizeof(x)) #define Mem1(x) memset(x, -1, sizeof(x)) #define Unique(v) v.resize(unique(All(v)) - v.begin()) #define peq(p0, p1) (p0.first == p1.first && p0.second == p1.second) #define End '\n' #define Out(x) cout << (x) << End #define OutP(p, sep, end) cout << p.first << sep << p.second << end template <typename T> inline bool Max(T &x, const T &y) { return x < y ? x = y, 1 : 0; } template <typename T> inline bool Min(T &x, const T &y) { return x > y ? x = y, 1 : 0; } template <typename T> using V = vector<T>; template <typename T> using VV = V<V<T>>; #define pb push_back #define mp make_pair #define X first #define Y second #define lb std::lower_bound #define ub std::upper_bound #define lbi(v, x) lb(All(v), (x)) - v.begin() #define ubi(v, x) ub(All(v), (x)) - v.begin() static const ll MOD = 1e9 + 7; static const double PI = 3.141592653589793; #define LOCAL 1 int main() { #if LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif cin.tie(0); ios::sync_with_stdio(false); // std::cout.precision(18); int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0, min(a, b) - (n - max(a, b))) << End; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define Abs(x) ((x) < 0 ? (x) * -1 : (x)) #define rep(x, y) for ((x) = 0; (x) < (y); (x)++) #define repin(x, y) for ((x) = 0; (x) <= (y); (x)++) #define nep(x, y) for ((x) = (y)-1; 0 <= (x); (x)--) #define nepi(x, y, z) for ((x) = (y)-1; (z) <= (x); (x)--) #define repi(x, y, z) for ((x) = (z); (x) < (y); (x)++) #define repiin(x, y, z) for ((x) = (z); (x) <= (y); (x)++) #define reps(x, y, z) for ((x) = 0; (x) < (y); (x) += (z)) #define repis(x, y, z, s) for ((x) = (z); (x) < (y); (x) += (s)) #define repiins(x, y, z, s) for ((x) = (z); (x) <= (y); (x) += (s)) #define repit(x) \ for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++) #define nepit(x) \ for (__typeof((x).rbegin()) it = (x).rbegin(); it != (x).rend(); it++) #define All(x) (x).begin(), (x).end() #define Mem0(x) memset(x, 0, sizeof(x)) #define Mem1(x) memset(x, -1, sizeof(x)) #define Unique(v) v.resize(unique(All(v)) - v.begin()) #define peq(p0, p1) (p0.first == p1.first && p0.second == p1.second) #define End '\n' #define Out(x) cout << (x) << End #define OutP(p, sep, end) cout << p.first << sep << p.second << end template <typename T> inline bool Max(T &x, const T &y) { return x < y ? x = y, 1 : 0; } template <typename T> inline bool Min(T &x, const T &y) { return x > y ? x = y, 1 : 0; } template <typename T> using V = vector<T>; template <typename T> using VV = V<V<T>>; #define pb push_back #define mp make_pair #define X first #define Y second #define lb std::lower_bound #define ub std::upper_bound #define lbi(v, x) lb(All(v), (x)) - v.begin() #define ubi(v, x) ub(All(v), (x)) - v.begin() static const ll MOD = 1e9 + 7; static const double PI = 3.141592653589793; #define LOCAL 0 int main() { #if LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif cin.tie(0); ios::sync_with_stdio(false); // std::cout.precision(18); int n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max(0, min(a, b) - (n - max(a, b))) << End; return 0; }
replace
58
59
58
59
0
p03139
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define MAX 200005 #define MOD 998244353 #define pb push_back #define mp make_pair #define f first #define s second ll n, m, i, j, k, t, l, r, tmp, ans, cnt, curr, flag, sum, mn, mx, mxi, x, pre, strt, en; ll brr[MAX], arr[MAX], dp[101], dpp[101]; string s; vector<ll> v; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif cin >> n >> l >> r; cout << min(l, r) << " " << max((l + r) - n, 0ll); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define MAX 200005 #define MOD 998244353 #define pb push_back #define mp make_pair #define f first #define s second ll n, m, i, j, k, t, l, r, tmp, ans, cnt, curr, flag, sum, mn, mx, mxi, x, pre, strt, en; ll brr[MAX], arr[MAX], dp[101], dpp[101]; string s; vector<ll> v; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> l >> r; cout << min(l, r) << " " << max((l + r) - n, 0ll); return 0; }
replace
20
23
20
21
0
p03139
C++
Runtime Error
#include <bits/stdc++.h> #include <chrono> #define rep(i, a, b) for (int i = a; i < b; i++) #define rep2(i, a, b) for (int i = a; i >= b; i--) #define mod 1000000007 #define INF 1e9 + 5 #define fr first #define se second #define int long long #define ll long long #define PI 3.1415926535 #define pll pair<ll, ll> #define vl vector<ll> #define mll map<ll, ll> #define mci map<char, int> #define msi map<string, int> #define vb vector<bool> #define pb push_back #define mpr make_pair #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define Senky_Bansal ios_base::sync_with_stdio(false); #define IIIT_ALLAHABAD \ cin.tie(NULL); \ cout.tie(NULL); using namespace std; const int N = 100005; vector<ll> v[26]; signed main() { Senky_Bansal IIIT_ALLAHABAD #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max((ll)0, a + b - n) << endl; }
#include <bits/stdc++.h> #include <chrono> #define rep(i, a, b) for (int i = a; i < b; i++) #define rep2(i, a, b) for (int i = a; i >= b; i--) #define mod 1000000007 #define INF 1e9 + 5 #define fr first #define se second #define int long long #define ll long long #define PI 3.1415926535 #define pll pair<ll, ll> #define vl vector<ll> #define mll map<ll, ll> #define mci map<char, int> #define msi map<string, int> #define vb vector<bool> #define pb push_back #define mpr make_pair #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define Senky_Bansal ios_base::sync_with_stdio(false); #define IIIT_ALLAHABAD \ cin.tie(NULL); \ cout.tie(NULL); using namespace std; const int N = 100005; vector<ll> v[26]; signed main() { Senky_Bansal IIIT_ALLAHABAD ll n, a, b; cin >> n >> a >> b; cout << min(a, b) << " " << max((ll)0, a + b - n) << endl; }
replace
29
36
29
30
0
p03140
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(int, char **) { unsigned int n, ans = 0; string a, b, c; cin >> n >> a >> b >> c; for (unsigned int i = 0; i < n; i++) { if (a[i] == b[i] && a[i] == c[i]) continue; if (a[i] == b[i]) ++ans; else if (a[i] == c[i]) ++ans; else if (b[i] == c[i]) ++ans; else ans += 2; // cerr << ans << " "; } cout << ans << endl; return 1; }
#include <bits/stdc++.h> using namespace std; int main(int, char **) { unsigned int n, ans = 0; string a, b, c; cin >> n >> a >> b >> c; for (unsigned int i = 0; i < n; i++) { if (a[i] == b[i] && a[i] == c[i]) continue; if (a[i] == b[i]) ++ans; else if (a[i] == c[i]) ++ans; else if (b[i] == c[i]) ++ans; else ans += 2; // cerr << ans << " "; } cout << ans << endl; return 0; }
replace
24
25
24
25
1
p03140
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define ll long long using namespace std; int main() { int n; cin >> n; vector<string> abc(n); rep(i, 3) cin >> abc[i]; int result = 0; rep(i, n) { set<char> s; rep(j, 3) s.insert(abc[j][i]); result += (s.size() - 1); } cout << result; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define ll long long using namespace std; int main() { int n; cin >> n; vector<string> abc(3); rep(i, 3) cin >> abc[i]; int result = 0; rep(i, n) { set<char> s; rep(j, 3) s.insert(abc[j][i]); result += (s.size() - 1); } cout << result; return 0; }
replace
10
11
10
11
0
p03140
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse2") #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define FF first #define SS second #define sz(x) (int)x.size() #define endl '\n' #define hell 1000000007 #define rep(i, a, b) for (int i = a; i < b; i++) int solve() { int n; string a, b, c; cin >> n >> a >> b >> c; int count = 0; rep(i, 0, n) { char a1 = a[i], b1 = b[i], c1 = c[i]; if (a1 == b1 && b1 == c1) continue; else if (a1 == b1 && b1 != c1) count += 1; else if (a1 == c1 && c1 != b1) count += 1; else if (b1 == c1 && c1 != a1) count += 1; else count += 2; } cout << count; return 0; } int main() { int t = 1; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif clock_t time_req; // Without using pow function time_req = clock(); // cin>>t; bool flag = true; while (t--) { solve(); } time_req = clock() - time_req; // cout << "Processor time taken is " // << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse2") #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define FF first #define SS second #define sz(x) (int)x.size() #define endl '\n' #define hell 1000000007 #define rep(i, a, b) for (int i = a; i < b; i++) int solve() { int n; string a, b, c; cin >> n >> a >> b >> c; int count = 0; rep(i, 0, n) { char a1 = a[i], b1 = b[i], c1 = c[i]; if (a1 == b1 && b1 == c1) continue; else if (a1 == b1 && b1 != c1) count += 1; else if (a1 == c1 && c1 != b1) count += 1; else if (b1 == c1 && c1 != a1) count += 1; else count += 2; } cout << count; return 0; } int main() { int t = 1; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); clock_t time_req; // Without using pow function time_req = clock(); // cin>>t; bool flag = true; while (t--) { solve(); } time_req = clock() - time_req; // cout << "Processor time taken is " // << (double)time_req/CLOCKS_PER_SEC << " seconds" << endl; return 0; }
delete
52
57
52
52
-11
p03140
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <map> #include <set> #include <string> #include <vector> #define X first #define Y second #define vsort(v) sort((v).begin(), (v).end()) #define vrev(v) reverse((v).begin(), (v).end()) // stoi(s): string to int // stod(s): string to double // INT_MAX // INT_MIN // LLONG_MAX // LLONG_MIN // DBL_MIN // DBL_MAX // LDBL_MIN // LDBL_MAX // A-Z: 65~90 // a-z: 97~122 // |a-z| = 26 using namespace std; using ll = long long; int main(int argc, const char *argv[]) { int n; cin >> n; vector<string> v(n); for (int i = 0; i < 3; i++) { cin >> v[i]; } int cnt = 0; set<char> st; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { st.insert(v[j][i]); } cnt += st.size() - 1; st.clear(); } cout << cnt << endl; }
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <map> #include <set> #include <string> #include <vector> #define X first #define Y second #define vsort(v) sort((v).begin(), (v).end()) #define vrev(v) reverse((v).begin(), (v).end()) // stoi(s): string to int // stod(s): string to double // INT_MAX // INT_MIN // LLONG_MAX // LLONG_MIN // DBL_MIN // DBL_MAX // LDBL_MIN // LDBL_MAX // A-Z: 65~90 // a-z: 97~122 // |a-z| = 26 using namespace std; using ll = long long; int main(int argc, const char *argv[]) { int n; cin >> n; vector<string> v(3); for (int i = 0; i < 3; i++) { cin >> v[i]; } int cnt = 0; set<char> st; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { st.insert(v[j][i]); } cnt += st.size() - 1; st.clear(); } cout << cnt << endl; }
replace
37
38
37
38
0
p03140
C++
Runtime Error
#include <iostream> using namespace std; int main(void) { int n; cin >> n; string s[n]; for (int i = 0; i < 3; i++) { cin >> s[i]; } long sum = 0; for (int i = 0; i < n; i++) { if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) { } else if (s[0][i] == s[1][i] || s[1][i] == s[2][i] || s[0][i] == s[2][i]) { sum += 1; } else { sum += 2; } } cout << sum << endl; return 0; }
#include <iostream> using namespace std; int main(void) { int n; cin >> n; string s[3]; for (int i = 0; i < 3; i++) { cin >> s[i]; } long sum = 0; for (int i = 0; i < n; i++) { if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) { } else if (s[0][i] == s[1][i] || s[1][i] == s[2][i] || s[0][i] == s[2][i]) { sum += 1; } else { sum += 2; } } cout << sum << endl; return 0; }
replace
5
6
5
6
0
p03140
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(var, n) for (decltype(n) var = 0; var < (n); var++) #define RREP(var, n) \ for (auto var = n - 1; var != static_cast<decltype(var)>(-1); var--) #define FOR(var, a, b) for (auto var = (a); var < (b); var++) #define RFOR(var, a, b) for (auto var = b - 1; var != a; var--) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; #define all(c) begin(c), end(c) #define D(x) \ do { \ std::cerr << x; \ } while (0) #define D2(x) \ do { \ std::cerr << #x << ": " << x << std::endl; \ } while (0) template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { out << '['; std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", ")); out << "\b\b]"; } return out; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } auto solve() { int n; cin >> n; vector<string> st(n); for (auto &&e : st) cin >> e; int res = 0; REP(j, n) { set<char> diff; REP(i, 3) { diff.insert(st[i][j]); } res += diff.size() - 1; } return res; } int main() { cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); // cout << (solve() ? "Yes" : "No") << endl; // cout << (solve() ? "YES" : "NO") << endl; cout << solve() << endl; // solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(var, n) for (decltype(n) var = 0; var < (n); var++) #define RREP(var, n) \ for (auto var = n - 1; var != static_cast<decltype(var)>(-1); var--) #define FOR(var, a, b) for (auto var = (a); var < (b); var++) #define RFOR(var, a, b) for (auto var = b - 1; var != a; var--) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; #define all(c) begin(c), end(c) #define D(x) \ do { \ std::cerr << x; \ } while (0) #define D2(x) \ do { \ std::cerr << #x << ": " << x << std::endl; \ } while (0) template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { out << '['; std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", ")); out << "\b\b]"; } return out; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } auto solve() { int n; cin >> n; vector<string> st(3); for (auto &&e : st) cin >> e; int res = 0; REP(j, n) { set<char> diff; REP(i, 3) { diff.insert(st[i][j]); } res += diff.size() - 1; } return res; } int main() { cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); // cout << (solve() ? "Yes" : "No") << endl; // cout << (solve() ? "YES" : "NO") << endl; cout << solve() << endl; // solve(); return 0; }
replace
44
45
44
45
0
p03140
C++
Runtime Error
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #define pp pair<int, int> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define ld long double #define al(a) (a).begin(), (a).end() #define mk make_pair #define check cout << "?" << endl; ll MOD = 1000000007; ll mod = 998244353; int inf = 1000001000; ll INF = 1e18 + 5; int main() { int n, ans = 0; cin >> n; vector<string> s(3); rep(i, 3) cin >> s[i]; rep(i, n) { int cnt = 0; rep(j, 3) if (s[i][j % 3] == s[i][(j + 1) % 3]) cnt++; if (cnt == 0) ans += 2; else if (cnt == 1) ans++; } cout << ans << endl; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #define pp pair<int, int> #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define ld long double #define al(a) (a).begin(), (a).end() #define mk make_pair #define check cout << "?" << endl; ll MOD = 1000000007; ll mod = 998244353; int inf = 1000001000; ll INF = 1e18 + 5; int main() { int n, ans = 0; cin >> n; vector<string> s(3); rep(i, 3) cin >> s[i]; rep(i, n) { int cnt = 0; rep(j, 3) if (s[j % 3][i] == s[(j + 1) % 3][i]) cnt++; if (cnt == 0) ans += 2; else if (cnt == 1) ans++; } cout << ans << endl; }
replace
23
24
23
24
-11
p03141
C++
Time Limit Exceeded
#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <math.h> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; using pii = pair<int, int>; using ll = long long; using ld = long double; #define pb push_back #define mp make_pair #define sc second #define fr first #define stpr setprecision #define cYES cout << "YES" << endl #define cNO cout << "NO" << endl #define cYes cout << "Yes" << endl #define cNo cout << "No" << endl #define rep(i, n) for (ll i = 0; i < (n); ++i) #define Rep(i, a, b) for (ll i = (a); i < (b); ++i) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define rRep(i, a, b) for (int i = a; i >= b; i--) #define crep(i) for (char i = 'a'; i <= 'z'; ++i) #define psortsecond(A, N) \ sort(A, A + N, \ [](const pii &a, const pii &b) { return a.second < b.second; }); #define ALL(x) (x).begin(), (x).end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) { \ cout << x << ' '; \ } \ cout << endl; #define endl '\n' int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } constexpr ll MOD = 1000000007; constexpr ll INF = 1000000011; constexpr ll MOD2 = 998244353; constexpr ll LINF = 1001002003004005006ll; constexpr ld EPS = 10e-8; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &&x : v) is >> x; return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first; is >> p.second; return is; } template <typename T, typename U> ostream &operator>>(ostream &os, const pair<T, U> &p) { os << p.first << ' ' << p.second; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) { if (i != begin(v)) os << ' '; os << *i; } return os; } ll T[100007]; int main() { ll N, A, B; pair<ll, ll> C[100007], D[100007]; ll sum = 0; cin >> N; rep(i, N) { cin >> A >> B; sum -= B; C[i].fr = A; C[i].sc = B; } sort(C, C + N, [](pair<ll, ll> &a, pair<ll, ll> &b) { return a.fr + a.sc >= b.fr + b.sc; }); rep(i, N) { if (i % 2 == 0) { sum += C[i].fr + C[i].sc; } } cout << sum << endl; }
#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <math.h> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; using pii = pair<int, int>; using ll = long long; using ld = long double; #define pb push_back #define mp make_pair #define sc second #define fr first #define stpr setprecision #define cYES cout << "YES" << endl #define cNO cout << "NO" << endl #define cYes cout << "Yes" << endl #define cNo cout << "No" << endl #define rep(i, n) for (ll i = 0; i < (n); ++i) #define Rep(i, a, b) for (ll i = (a); i < (b); ++i) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define rRep(i, a, b) for (int i = a; i >= b; i--) #define crep(i) for (char i = 'a'; i <= 'z'; ++i) #define psortsecond(A, N) \ sort(A, A + N, \ [](const pii &a, const pii &b) { return a.second < b.second; }); #define ALL(x) (x).begin(), (x).end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) { \ cout << x << ' '; \ } \ cout << endl; #define endl '\n' int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } constexpr ll MOD = 1000000007; constexpr ll INF = 1000000011; constexpr ll MOD2 = 998244353; constexpr ll LINF = 1001002003004005006ll; constexpr ld EPS = 10e-8; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &&x : v) is >> x; return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first; is >> p.second; return is; } template <typename T, typename U> ostream &operator>>(ostream &os, const pair<T, U> &p) { os << p.first << ' ' << p.second; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) { if (i != begin(v)) os << ' '; os << *i; } return os; } ll T[100007]; int main() { ll N, A, B; pair<ll, ll> C[100007], D[100007]; ll sum = 0; cin >> N; rep(i, N) { cin >> A >> B; sum -= B; C[i].fr = A; C[i].sc = B; } sort(C, C + N, [](pair<ll, ll> &a, pair<ll, ll> &b) { return a.fr + a.sc > b.fr + b.sc; }); rep(i, N) { if (i % 2 == 0) { sum += C[i].fr + C[i].sc; } } cout << sum << endl; }
replace
110
111
110
111
TLE
p03141
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; long long ans1, ans2, n; vector<pii> vec; bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; } bool flag = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; vec.push_back(pii(a, b)); } sort(vec.begin(), vec.end(), cmp); while (!vec.empty()) { if (!flag) { ans1 += vec.back().first; } else { ans2 += vec.back().second; } flag = !flag; } cout << ans1 - ans2; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; long long ans1, ans2, n; vector<pii> vec; bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; } bool flag = 0; int main() { cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; vec.push_back(pii(a, b)); } sort(vec.begin(), vec.end(), cmp); while (!vec.empty()) { if (!flag) { ans1 += vec.back().first; } else { ans2 += vec.back().second; } flag = !flag; vec.pop_back(); } cout << ans1 - ans2; }
insert
23
23
23
24
TLE
p03141
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<pair<int, int>> vec(n); for (auto &v : vec) { int a, b; cin >> a >> b; v = make_pair(a, b); } sort(vec.begin(), vec.end(), [](auto &lhs, auto &rhs) { return (lhs.first + lhs.second) >= (rhs.first + rhs.second); }); ll ans = 0; for (int i = 0; i < n; ++i) ans += i % 2 == 0 ? vec[i].first : -vec[i].second; cout << ans << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<pair<int, int>> vec(n); for (auto &v : vec) { int a, b; cin >> a >> b; v = make_pair(a, b); } sort(vec.begin(), vec.end(), [](auto &lhs, auto &rhs) { return (lhs.first + lhs.second) > (rhs.first + rhs.second); }); ll ans = 0; for (int i = 0; i < n; ++i) ans += i % 2 == 0 ? vec[i].first : -vec[i].second; cout << ans << endl; }
replace
18
19
18
19
0
p03141
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++) using namespace std; typedef long long ll; int main(void) { ll N; cin >> N; vector<ll> A(N), B(N); REP(i, 0, N) cin >> A[i] >> B[i]; vector<ll> ord(N); REP(i, 0, N) ord[i] = i; sort(ord.begin(), ord.end(), [&](const ll i, const ll j) { return A[i] + B[i] >= A[j] + B[j]; }); ll ans = 0; REP(i, 0, N) { ll k = ord[i]; if (i % 2 == 0) { ans += A[k]; } else { ans -= B[k]; } } cout << ans << endl; }
#include <bits/stdc++.h> #define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++) using namespace std; typedef long long ll; int main(void) { ll N; cin >> N; vector<ll> A(N), B(N); REP(i, 0, N) cin >> A[i] >> B[i]; vector<ll> ord(N); REP(i, 0, N) ord[i] = i; sort(ord.begin(), ord.end(), [&](const ll i, const ll j) { return A[i] + B[i] < A[j] + B[j]; }); reverse(ord.begin(), ord.end()); ll ans = 0; REP(i, 0, N) { ll k = ord[i]; if (i % 2 == 0) { ans += A[k]; } else { ans -= B[k]; } } cout << ans << endl; }
replace
15
16
15
17
0
p03141
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int N; cin >> N; int A[100000], B[100000]; for (int i = 0; i < N; i++) cin >> A[i] >> B[i]; ll sum; pair<ll, int> C[10000]; for (int i = 0; i < N; i++) { sum = A[i] + B[i]; C[i] = make_pair(sum, i); } // 昇順ソート sort(C, C + N); int index; ll ans = 0; for (int i = 0; i < N; i++) { index = C[N - i - 1].second; ans += A[index]; i++; if (i == N) break; index = C[N - i - 1].second; ans -= B[index]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int N; cin >> N; int A[100000], B[100000]; for (int i = 0; i < N; i++) cin >> A[i] >> B[i]; ll sum; pair<ll, int> C[100000]; for (int i = 0; i < N; i++) { sum = A[i] + B[i]; C[i] = make_pair(sum, i); } // 昇順ソート sort(C, C + N); int index; ll ans = 0; for (int i = 0; i < N; i++) { index = C[N - i - 1].second; ans += A[index]; i++; if (i == N) break; index = C[N - i - 1].second; ans -= B[index]; } cout << ans << endl; return 0; }
replace
12
13
12
13
0
p03141
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; const int nmax = 100005; struct grm { long long a, b; } a[nmax]; bool comp(grm a, grm b) { return (a.a + b.a < b.a + b.b); } long long ans, sgn; int x, y, n, i; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i].a >> a[i].b; } sort(a + 1, a + n + 1, comp); sgn = 1; for (i = n; i >= 1; i--) { if (sgn == 1) ans += 1LL * a[i].a; else ans -= 1LL * a[i].b; sgn *= -1; } cout << ans; return 0; }
#include <algorithm> #include <iostream> using namespace std; const int nmax = 100005; struct grm { long long a, b; } a[nmax]; bool comp(grm unu, grm doi) { return (unu.a + unu.b < doi.a + doi.b); } long long ans, sgn; int x, y, n, i; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i].a >> a[i].b; } sort(a + 1, a + n + 1, comp); sgn = 1; for (i = n; i >= 1; i--) { if (sgn == 1) ans += 1LL * a[i].a; else ans -= 1LL * a[i].b; sgn *= -1; } cout << ans; return 0; }
replace
7
8
7
8
0
p03141
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using ll = long long; using ull = unsigned long long; using namespace std; #define FALSE printf("false\n"); #define TRUE printf("true\n"); #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; (i) < ((int)(n)); ++(i)) #define pb push_back #define mp make_pair #define fi first #define se second #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define test(a) \ cout << "line:" << __LINE__ << "[" << (#a) << ": " << (a) << "]" << endl const int INF = 1e9 + 7; const ll INFL = 9 * 1e18; // const int dx[4] = { 1, 0, -1, 0 }; // const int dy[4] = { 0, 1, 0, -1 }; const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int inline digit(ll num) { int tmp = 0; while (num) { tmp++; num /= 10; } return tmp; } // 桁数 template <typename T> inline T SUM(vector<T> vec) { return accumulate(all(vec), (T)0); } // vectorの中身を全部足す template <typename T> inline T digitSum(T num) { T sum = 0; while (num) { sum += num % 10; num /= 10; } return sum; } // 各桁の和 template <typename T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } // 最大公約数 template <typename T> inline T lcm(T a, T b) { T g = gcd(a, b); return a / g * b; } // 最小公倍数 template <typename T> inline T power(T a, T b) { T tmp = 1; rep(i, b) { tmp *= a; } return tmp; } // 累乗 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; } template <typename T> inline void print(T &&x) { cout << setprecision(32) << x << endl; } // TODO: write int main() { int n; cin >> n; vector<pair<int, pair<int, int>>> a(n); rep(i, n) { int l, r; cin >> l >> r; a[i].fi = l + r; a[i].se.fi = l; a[i].se.se = r; } sort(all(a), [](auto l, auto r) { return l.fi - r.fi; }); ll l = 0, r = 0; rep(i, n) { if (i % 2) { r += a[i].second.second; } else { l += a[i].second.first; } } print(l - r); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using ll = long long; using ull = unsigned long long; using namespace std; #define FALSE printf("false\n"); #define TRUE printf("true\n"); #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; (i) < ((int)(n)); ++(i)) #define pb push_back #define mp make_pair #define fi first #define se second #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define test(a) \ cout << "line:" << __LINE__ << "[" << (#a) << ": " << (a) << "]" << endl const int INF = 1e9 + 7; const ll INFL = 9 * 1e18; // const int dx[4] = { 1, 0, -1, 0 }; // const int dy[4] = { 0, 1, 0, -1 }; const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int inline digit(ll num) { int tmp = 0; while (num) { tmp++; num /= 10; } return tmp; } // 桁数 template <typename T> inline T SUM(vector<T> vec) { return accumulate(all(vec), (T)0); } // vectorの中身を全部足す template <typename T> inline T digitSum(T num) { T sum = 0; while (num) { sum += num % 10; num /= 10; } return sum; } // 各桁の和 template <typename T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } // 最大公約数 template <typename T> inline T lcm(T a, T b) { T g = gcd(a, b); return a / g * b; } // 最小公倍数 template <typename T> inline T power(T a, T b) { T tmp = 1; rep(i, b) { tmp *= a; } return tmp; } // 累乗 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; } template <typename T> inline void print(T &&x) { cout << setprecision(32) << x << endl; } // TODO: write int main() { int n; cin >> n; vector<pair<int, pair<int, int>>> a(n); rep(i, n) { int l, r; cin >> l >> r; a[i].fi = l + r; a[i].se.fi = l; a[i].se.se = r; } sort(all(a), [](auto l, auto r) { return l.fi > r.fi; }); ll l = 0, r = 0; rep(i, n) { if (i % 2) { r += a[i].second.second; } else { l += a[i].second.first; } } print(l - r); return 0; }
replace
103
104
103
104
0
p03141
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <map> #include <queue> #include <random> #include <vector> using namespace std; int N; pair<long long, pair<long long, long long>> P[100000]; int main(void) { long long a, b; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld %lld", &a, &b); P[i] = make_pair(a + b, make_pair(a, b)); } sort(P, P + N); long long ans = 0; for (int i = N - 1; i >= 0; i++) { if (i % 2 == (N - 1) % 2) { ans += P[i].second.first; } else { ans -= P[i].second.second; } } printf("%lld\n", ans); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <map> #include <queue> #include <random> #include <vector> using namespace std; int N; pair<long long, pair<long long, long long>> P[100000]; int main(void) { long long a, b; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld %lld", &a, &b); P[i] = make_pair(a + b, make_pair(a, b)); } sort(P, P + N); long long ans = 0; for (int i = N - 1; i >= 0; i--) { if (i % 2 == (N - 1) % 2) { ans += P[i].second.first; } else { ans -= P[i].second.second; } } printf("%lld\n", ans); }
replace
27
28
27
28
TLE
p03141
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; // #define int ll using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; } template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &a) { out << '(' << a.first << ',' << a.second << ')'; return out; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) { is >> x; } return is; } template <class T> ostream &operator<<(ostream &out, const vector<T> &a) { out << '['; for (T i : a) { out << i << ','; } out << ']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL const int INF = 1 << 30; const ll LLINF = 1LL << 60; const ll MOD = 1000000007; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> a(n), b(n); REP(i, n) cin >> a[i] >> b[i]; vector<ll> idx(n); iota(ALL(idx), 0); sort(ALL(idx), [&](ll l, ll r) { return a[l] + b[l] >= a[r] + b[r]; }); // cout << idx << endl; ll ret = 0; REP(i, n) { if (i % 2 == 0) { ret += a[idx[i]]; } else { ret -= b[idx[i]]; } } cout << ret << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // #define int ll using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; } template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <class S, class T> ostream &operator<<(ostream &out, const pair<S, T> &a) { out << '(' << a.first << ',' << a.second << ')'; return out; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) { is >> x; } return is; } template <class T> ostream &operator<<(ostream &out, const vector<T> &a) { out << '['; for (T i : a) { out << i << ','; } out << ']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL const int INF = 1 << 30; const ll LLINF = 1LL << 60; const ll MOD = 1000000007; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> a(n), b(n); REP(i, n) cin >> a[i] >> b[i]; vector<ll> idx(n); iota(ALL(idx), 0); sort(ALL(idx), [&](ll l, ll r) { return a[l] + b[l] < a[r] + b[r]; }); reverse(ALL(idx)); // cout << idx << endl; ll ret = 0; REP(i, n) { if (i % 2 == 0) { ret += a[idx[i]]; } else { ret -= b[idx[i]]; } } cout << ret << endl; return 0; }
replace
66
67
66
68
0
p03141
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct node { int a, b; int tot; }; node peo[50005]; int cmp(node x, node y) { if (x.tot > y.tot) return true; return false; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> peo[i].a >> peo[i].b; peo[i].tot = peo[i].a + peo[i].b; } sort(peo + 1, peo + n + 1, cmp); long long ans = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1) ans += peo[i].a; else ans -= peo[i].b; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int a, b; int tot; }; node peo[500005]; int cmp(node x, node y) { if (x.tot > y.tot) return true; return false; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> peo[i].a >> peo[i].b; peo[i].tot = peo[i].a + peo[i].b; } sort(peo + 1, peo + n + 1, cmp); long long ans = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1) ans += peo[i].a; else ans -= peo[i].b; } cout << ans; return 0; }
replace
6
7
6
7
0
p03141
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <iostream> struct Dish { long long a; long long b; long long sum; }; int main() { std::size_t n; std::cin >> n; std::array<Dish, 100000> dishVec; for (std::size_t i = 0; i < n; i++) { std::cin >> dishVec[i].a >> dishVec[i].b; dishVec[i].sum = dishVec[i].a + dishVec[i].b; } std::sort(dishVec.begin(), dishVec.begin() + n, [](auto d1, auto d2) { return d1.sum >= d2.sum; }); long long result = 0; for (std::size_t i = 0; i < n; i++) { if (i % 2 == 0) result += dishVec[i].a; else result -= dishVec[i].b; } std::cout << result << std::endl; return 0; }
#include <algorithm> #include <array> #include <iostream> struct Dish { long long a; long long b; long long sum; }; int main() { std::size_t n; std::cin >> n; std::array<Dish, 100000> dishVec; for (std::size_t i = 0; i < n; i++) { std::cin >> dishVec[i].a >> dishVec[i].b; dishVec[i].sum = dishVec[i].a + dishVec[i].b; } std::sort(dishVec.begin(), dishVec.begin() + n, [](auto d1, auto d2) { return d2.sum < d1.sum; }); long long result = 0; for (std::size_t i = 0; i < n; i++) { if (i % 2 == 0) result += dishVec[i].a; else result -= dishVec[i].b; } std::cout << result << std::endl; return 0; }
replace
17
18
17
18
TLE
p03141
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> const double PI = 3.14159265358979323846; // using namespace boost::multiprecision; using namespace std; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 1e15; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; std::istream &operator>>(std::istream &in, set<int> &o) { ll a; in >> a; o.insert(a); return in; } std::istream &operator>>(std::istream &in, queue<int> &o) { ll a; in >> a; o.push(a); return in; } bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } // ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt"); // ofstream outfile("log.txt"); // outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') // << rank << endl; // std::cout << std::bitset<8>(9); const int mod = 1000000007; // const ll mod = 1e10; typedef priority_queue<string, vector<string>, greater<string>> PQ_ASK; struct Happy { ll takahashi, aoki, takahashi_value; }; int main() { int n; cin >> n; vector<Happy> happiness(n); rep(i, n) cin >> happiness[i].takahashi >> happiness[i].aoki; rep(i, n) happiness[i].takahashi_value = happiness[i].takahashi + happiness[i].aoki; sort(happiness.rbegin(), happiness.rend(), [](Happy &h, Happy i) { return h.takahashi_value; }); ll takahashi = 0, aoki = 0; rep(i, n) { if (i % 2 == 0) { takahashi += happiness[i].takahashi; } else { aoki += happiness[i].aoki; } } cout << takahashi - aoki << endl; }
#include <bits/stdc++.h> #include <cmath> const double PI = 3.14159265358979323846; // using namespace boost::multiprecision; using namespace std; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 1e15; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; std::istream &operator>>(std::istream &in, set<int> &o) { ll a; in >> a; o.insert(a); return in; } std::istream &operator>>(std::istream &in, queue<int> &o) { ll a; in >> a; o.push(a); return in; } bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } // ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt"); // ofstream outfile("log.txt"); // outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') // << rank << endl; // std::cout << std::bitset<8>(9); const int mod = 1000000007; // const ll mod = 1e10; typedef priority_queue<string, vector<string>, greater<string>> PQ_ASK; struct Happy { ll takahashi, aoki, takahashi_value; }; int main() { int n; cin >> n; vector<Happy> happiness(n); rep(i, n) cin >> happiness[i].takahashi >> happiness[i].aoki; rep(i, n) happiness[i].takahashi_value = happiness[i].takahashi + happiness[i].aoki; sort(happiness.rbegin(), happiness.rend(), [](Happy &h, Happy &i) { return h.takahashi_value < i.takahashi_value; }); ll takahashi = 0, aoki = 0; rep(i, n) { if (i % 2 == 0) { takahashi += happiness[i].takahashi; } else { aoki += happiness[i].aoki; } } cout << takahashi - aoki << endl; }
replace
53
55
53
56
0
p03141
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define INF 1000000000 #define LINF 9000000000000000000 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define REP(i, a, b) for (int i = (a); i < int(b); i++) #define all(x) (x).begin(), x.end() #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<long long> vll; typedef pair<int, int> pi; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; bool debug = false; /*---------------------------------------------------*/ bool comp(pair<ll, pair<ll, int>> a, pair<ll, pair<ll, int>> b) { return (a.first + a.second.first < b.first + b.second.first); } int main() { int N; cin >> N; vector<pair<ll, int>> A(N), B(N); vector<pair<ll, pair<ll, int>>> data; pair<ll, pair<ll, int>> a; rep(i, N) { cin >> A[i].first >> B[i].first; A[i].second = B[i].second = i; a.first = A[i].first; a.second.first = B[i].first; a.second.second = i; data.pb(a); } sort(all(data), comp); reverse(all(data)); vector<bool> used(N + 1); ll cnt = 0, sum_a = 0, sum_b = 0; for (int i = 0; i < N * 2; i++) { if (!used[data[i].second.second]) { if (cnt % 2 == 0) { sum_a += A[data[i].second.second].first; } else { sum_b += B[data[i].second.second].first; } used[data[i].second.second] = true; cnt++; } } cout << sum_a - sum_b << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define INF 1000000000 #define LINF 9000000000000000000 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define REP(i, a, b) for (int i = (a); i < int(b); i++) #define all(x) (x).begin(), x.end() #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<long long> vll; typedef pair<int, int> pi; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; bool debug = false; /*---------------------------------------------------*/ bool comp(pair<ll, pair<ll, int>> a, pair<ll, pair<ll, int>> b) { return (a.first + a.second.first < b.first + b.second.first); } int main() { int N; cin >> N; vector<pair<ll, int>> A(N), B(N); vector<pair<ll, pair<ll, int>>> data; pair<ll, pair<ll, int>> a; rep(i, N) { cin >> A[i].first >> B[i].first; A[i].second = B[i].second = i; a.first = A[i].first; a.second.first = B[i].first; a.second.second = i; data.pb(a); } sort(all(data), comp); reverse(all(data)); vector<bool> used(N + 1); ll cnt = 0, sum_a = 0, sum_b = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { sum_a += data[i].first; } else { sum_b += data[i].second.first; } } cout << sum_a - sum_b << endl; return 0; }
replace
75
84
75
80
0
p03141
C++
Runtime Error
#include <bits/stdc++.h> #define show(x) std::cerr << #x << " = " << (x) << std::endl using ll = long long; template <typename T> constexpr T MOD = static_cast<T>(1000000007); template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 16; int main() { int N; std::cin >> N; using P = std::pair<ll, ll>; std::vector<P> A(N); for (int i = 0; i < N; i++) { std::cin >> A[i].first >> A[i].second; } std::sort(A.begin(), A.end(), [](const P &p1, const P &p2) { return p1.first + p1.second > p2.first * p2.second; }); ll ans = 0; for (int i = 0; i < N; i++) { ans += (i % 2 == 0 ? A[i].first : -A[i].second); } std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> #define show(x) std::cerr << #x << " = " << (x) << std::endl using ll = long long; template <typename T> constexpr T MOD = static_cast<T>(1000000007); template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 16; int main() { int N; std::cin >> N; using P = std::pair<ll, ll>; std::vector<P> A(N); for (int i = 0; i < N; i++) { std::cin >> A[i].first >> A[i].second; } std::sort(A.begin(), A.end(), [](const P &p1, const P &p2) { return p1.first + p1.second > p2.first + p2.second; }); ll ans = 0; for (int i = 0; i < N; i++) { ans += (i % 2 == 0 ? A[i].first : -A[i].second); } std::cout << ans << std::endl; return 0; }
replace
14
15
14
15
0
p03141
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <bit> #include <bitset> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> typedef long double ld; typedef long long ll; using namespace std; struct dish { ll x, y; dish(ll tx = 0, ll ty = 0) { x = tx; y = ty; } void read() { cin >> x >> y; } bool operator<(const dish &d) const { return (x + y >= d.x + d.y); } }; #define MAXN (1 << 17) dish a[MAXN]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { a[i].read(); } sort(a, a + n); ll ret = 0; for (int i = 0; i < n; i++) { if (i & 1) { ret -= a[i].y; } else { ret += a[i].x; } } cout << ret << endl; return 0; }
#include <algorithm> #include <assert.h> #include <bit> #include <bitset> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> typedef long double ld; typedef long long ll; using namespace std; struct dish { ll x, y; dish(ll tx = 0, ll ty = 0) { x = tx; y = ty; } void read() { cin >> x >> y; } bool operator<(const dish &d) const { return (x + y > d.x + d.y); } }; #define MAXN (1 << 17) dish a[MAXN]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { a[i].read(); } sort(a, a + n); ll ret = 0; for (int i = 0; i < n; i++) { if (i & 1) { ret -= a[i].y; } else { ret += a[i].x; } } cout << ret << endl; return 0; }
replace
31
32
31
32
TLE
p03141
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define PI acos(-1.0) #define EPS 1e-8 #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define fi first #define se second #define go(i, a, b) for (int i = a; i <= b; ++i) #define gorev(i, a, b) for (int i = a; i >= b; --i) typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vector<int>> vvi; const ll mod = (ll)1e9 + 7; template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.fi << "," << P.se << ")"; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { os << "["; for (auto pr : M) os << pr.fi << "->" << pr.se << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, unordered_map<L, R> M) { os << "["; for (auto pr : M) os << pr.fi << "->" << pr.se << ","; return os << "]"; } mt19937_64 mt(time(0)); struct Matrix { vvi data; int r, c; Matrix(int row, int col, bool identity = false) : r(row), c(col) { data.assign(row, vector<int>(col, 0)); if (identity) { for (int i = 0; i < r; ++i) { data[i][i] = 1; } } } Matrix operator*(Matrix &other) { int m = r, n = c, p = other.c; Matrix res(m, p); for (int i = 0; i < m; ++i) { for (int j = 0; j < p; ++j) { for (int k = 0; k < n; ++k) { res.data[i][j] += data[i][k] * other.data[k][j]; } } } return res; } }; vector<int> extendGcd(int a, int b) { if (b == 0) { return {1, 0, a}; } else { vector<int> tmp = extendGcd(b, a % b); return {tmp[1], tmp[0] - (a / b) * tmp[1], tmp[2]}; } } Matrix matrix_power(Matrix base, ll exp) { int n = base.r; Matrix res(n, n, true); while (exp) { if (exp & 1) { res = res * base; } base = base * base; exp >>= 1; } return res; } inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline double dbread() { double X = 0, Y = 1.0; int w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar(); ch = getchar(); // 读入小数点 while (isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar(); return w ? -X : X; } /************************* ******* template ******** ************************/ #define N 100010 ll n; struct node { ll a; ll b; ll c; }; node arr[N]; bool cmp(node n1, node n2) { return n1.c >= n2.c; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout << fixed; #ifdef ZYC_LOCAL freopen("input.txt", "rt", stdin); #endif cin >> n; go(i, 0, n - 1) { cin >> arr[i].a >> arr[i].b; arr[i].c = arr[i].a + arr[i].b; } sort(arr, arr + n, cmp); ll res = 0; go(i, 0, n - 1) { if (i % 2) { res -= arr[i].b; } else { res += arr[i].a; } } cout << res; #ifdef ZYC_LOCAL cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define PI acos(-1.0) #define EPS 1e-8 #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define fi first #define se second #define go(i, a, b) for (int i = a; i <= b; ++i) #define gorev(i, a, b) for (int i = a; i >= b; --i) typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vector<int>> vvi; const ll mod = (ll)1e9 + 7; template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.fi << "," << P.se << ")"; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { os << "["; for (auto pr : M) os << pr.fi << "->" << pr.se << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, unordered_map<L, R> M) { os << "["; for (auto pr : M) os << pr.fi << "->" << pr.se << ","; return os << "]"; } mt19937_64 mt(time(0)); struct Matrix { vvi data; int r, c; Matrix(int row, int col, bool identity = false) : r(row), c(col) { data.assign(row, vector<int>(col, 0)); if (identity) { for (int i = 0; i < r; ++i) { data[i][i] = 1; } } } Matrix operator*(Matrix &other) { int m = r, n = c, p = other.c; Matrix res(m, p); for (int i = 0; i < m; ++i) { for (int j = 0; j < p; ++j) { for (int k = 0; k < n; ++k) { res.data[i][j] += data[i][k] * other.data[k][j]; } } } return res; } }; vector<int> extendGcd(int a, int b) { if (b == 0) { return {1, 0, a}; } else { vector<int> tmp = extendGcd(b, a % b); return {tmp[1], tmp[0] - (a / b) * tmp[1], tmp[2]}; } } Matrix matrix_power(Matrix base, ll exp) { int n = base.r; Matrix res(n, n, true); while (exp) { if (exp & 1) { res = res * base; } base = base * base; exp >>= 1; } return res; } inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline double dbread() { double X = 0, Y = 1.0; int w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar(); ch = getchar(); // 读入小数点 while (isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar(); return w ? -X : X; } /************************* ******* template ******** ************************/ #define N 100010 ll n; struct node { ll a; ll b; ll c; }; node arr[N]; bool cmp(node n1, node n2) { return n1.c > n2.c; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout << fixed; #ifdef ZYC_LOCAL freopen("input.txt", "rt", stdin); #endif cin >> n; go(i, 0, n - 1) { cin >> arr[i].a >> arr[i].b; arr[i].c = arr[i].a + arr[i].b; } sort(arr, arr + n, cmp); ll res = 0; go(i, 0, n - 1) { if (i % 2) { res -= arr[i].b; } else { res += arr[i].a; } } cout << res; #ifdef ZYC_LOCAL cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
replace
143
144
143
144
TLE
p03141
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++) #define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--) #define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define fill0(ar) fill((ar), 0) #define debug(x) cout << #x << ": " << x << endl #define ld double #define pb push_back #define mp make_pair #define ff first #define ss second typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<ii> vii; typedef vector<int> vi; #define INF 1000000000000000000 ll ar[100007]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long double pi = 3.14159265358979323846; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n; cin >> n; ll ans = 0; vector<ll> v1, v2, v3; rep(i, n) { ll x, y; cin >> x >> y; v1.pb(x + y); ans -= (y); } // debug(ans); sort(v1.begin(), v1.end()); reverse(v1.begin(), v1.end()); rep(i, n) { if (i % 2 == 0) ans += v1[i]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++) #define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--) #define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++) #define repr(i, n) for (ll i = n - 1; i >= 0; i--) #define fill(ar, val) memset(ar, val, sizeof(ar)) #define fill0(ar) fill((ar), 0) #define debug(x) cout << #x << ": " << x << endl #define ld double #define pb push_back #define mp make_pair #define ff first #define ss second typedef pair<int, int> ii; typedef pair<ii, int> iii; typedef vector<ii> vii; typedef vector<int> vi; #define INF 1000000000000000000 ll ar[100007]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long double pi = 3.14159265358979323846; ll n; cin >> n; ll ans = 0; vector<ll> v1, v2, v3; rep(i, n) { ll x, y; cin >> x >> y; v1.pb(x + y); ans -= (y); } // debug(ans); sort(v1.begin(), v1.end()); reverse(v1.begin(), v1.end()); rep(i, n) { if (i % 2 == 0) ans += v1[i]; } cout << ans; return 0; }
delete
30
34
30
30
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03141
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; // ll a[n],b[n]; ll ans = 0; pair<ll, int> s[n]; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; // a--;b--; s[i] = {a + b, i}; ans -= b; } sort(s, s + n); reverse(s, s + n); for (int i = 0; i < n; i += 2) { ans += s[i].first; } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { /* #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif */ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; // ll a[n],b[n]; ll ans = 0; pair<ll, int> s[n]; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; // a--;b--; s[i] = {a + b, i}; ans -= b; } sort(s, s + n); reverse(s, s + n); for (int i = 0; i < n; i += 2) { ans += s[i].first; } cout << ans; }
replace
5
9
5
10
-11
p03141
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int n; int a[10010], b[10010]; long long s[10010]; vector<pair<int, int>> V; long long answer = 0; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; s[i] = a[i] + b[i]; V.push_back(make_pair(s[i], i)); } sort(V.begin(), V.end()); while (V.size() != 0) { answer += a[V.back().second]; V.pop_back(); if (V.size() != 0) { answer -= b[V.back().second]; V.pop_back(); } } cout << answer << endl; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int n; long long a[100100], b[100100]; long long s[100100]; vector<pair<int, int>> V; long long answer = 0; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; s[i] = a[i] + b[i]; V.push_back(make_pair(s[i], i)); } sort(V.begin(), V.end()); while (V.size() != 0) { answer += a[V.back().second]; V.pop_back(); if (V.size() != 0) { answer -= b[V.back().second]; V.pop_back(); } } cout << answer << endl; }
replace
8
10
8
10
0
p03142
Python
Runtime Error
#!/usr/bin/env python3 N, M = map(int, input().split()) g = [[] for _ in range(N)] rg = [[] for _ in range(N)] for _ in range(N - 1 + M): A, B = (int(x) - 1 for x in input().split()) g[A].append(B) rg[B].append(A) def dfs(s): global ts global used used[s] = True for t in g[s]: if not used[t]: dfs(t) ts.append(s) def tsort(): global ts for i in range(N): dfs(i) ts = ts[::-1] used = [False] * N ts = [] tsort() mp = [None] * N for i, x in enumerate(ts): mp[x] = i ans = [0] * N for t in ts[1:]: if rg[t]: ans[t] = ts[max(mp[s] for s in rg[t])] + 1 for x in ans: print(x)
#!/usr/bin/env python3 import sys sys.setrecursionlimit(100000) N, M = map(int, input().split()) g = [[] for _ in range(N)] rg = [[] for _ in range(N)] for _ in range(N - 1 + M): A, B = (int(x) - 1 for x in input().split()) g[A].append(B) rg[B].append(A) def dfs(s): global ts global used used[s] = True for t in g[s]: if not used[t]: dfs(t) ts.append(s) def tsort(): global ts for i in range(N): dfs(i) ts = ts[::-1] used = [False] * N ts = [] tsort() mp = [None] * N for i, x in enumerate(ts): mp[x] = i ans = [0] * N for t in ts[1:]: if rg[t]: ans[t] = ts[max(mp[s] for s in rg[t])] + 1 for x in ans: print(x)
insert
1
1
1
4
0
p03142
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; // 頂点数、変数 int v, e; const int MAX_N = 10010; // 隣接リスト vector<int> to[MAX_N]; vector<int> from[MAX_N]; bool used[MAX_N]; // トポロジカルソートされた数列 vector<int> tps; void dfs(int u) { if (used[u]) return; used[u] = true; for (int i : to[u]) dfs(i); // 帰りがけ順で追加 tps.push_back(u); } void tsort() { for (int i = 0; i < v; ++i) dfs(i); reverse(tps.begin(), tps.end()); } int main(void) { int n, m; cin >> n >> m; v = n; e = n + m - 1; for (int i = 0; i < e; ++i) { int s, t; cin >> s >> t; s--; t--; to[s].push_back(t); from[t].push_back(s); } tsort(); int rvs[v]; // rep(i,n)cout << tps[i] << endl; rep(i, v) { int t = tps[i]; rvs[t] = i; } // rep(i,n)cout << rvs[i] << endl; int ans[v]; rep(i, v) { if (i == tps[0]) { cout << 0 << endl; continue; } int mx = 0; for (int j : from[i]) { mx = max(rvs[j], mx); // cout << rvs[j] << endl; } cout << tps[mx] + 1 << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; // 頂点数、変数 int v, e; const int MAX_N = 100100; // 隣接リスト vector<int> to[MAX_N]; vector<int> from[MAX_N]; bool used[MAX_N]; // トポロジカルソートされた数列 vector<int> tps; void dfs(int u) { if (used[u]) return; used[u] = true; for (int i : to[u]) dfs(i); // 帰りがけ順で追加 tps.push_back(u); } void tsort() { for (int i = 0; i < v; ++i) dfs(i); reverse(tps.begin(), tps.end()); } int main(void) { int n, m; cin >> n >> m; v = n; e = n + m - 1; for (int i = 0; i < e; ++i) { int s, t; cin >> s >> t; s--; t--; to[s].push_back(t); from[t].push_back(s); } tsort(); int rvs[v]; // rep(i,n)cout << tps[i] << endl; rep(i, v) { int t = tps[i]; rvs[t] = i; } // rep(i,n)cout << rvs[i] << endl; int ans[v]; rep(i, v) { if (i == tps[0]) { cout << 0 << endl; continue; } int mx = 0; for (int j : from[i]) { mx = max(rvs[j], mx); // cout << rvs[j] << endl; } cout << tps[mx] + 1 << endl; } return 0; }
replace
6
7
6
7
0
p03142
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define m0(x) memset(x, 0, sizeof(x)) int dx4[4] = {1, 0, -1, 0}, dy4[4] = {0, 1, 0, -1}; int main() { int n, m; cin >> n >> m; // vector<int> a(n),b(n),parent(n),isroot(n),pcnt(n); vector<int> a(n + 5), b(n + 5), parent(n + 5), isroot(n + 5), pcnt(n + 5); vector<vector<int>> a2b(n); for (int i = 0; i < n - 1 + m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; a2b[a[i]].push_back(b[i]); isroot[b[i]] = -1; pcnt[b[i]]++; // oya nannnin iruka } int root; for (int i = 0; i < n; i++) { if (isroot[i] == 0) { root = i; parent[i] = 0; } else { parent[i] = -1; } } queue<int> qa; qa.push(root); while (qa.size() > 0) { int nowpar = qa.front(); qa.pop(); for (int i = 0; i < a2b[nowpar].size(); i++) { int nowchi = a2b[nowpar][i]; pcnt[nowchi]--; if (pcnt[nowchi] == 0) { parent[nowchi] = nowpar + 1; qa.push(nowchi); } } } for (int i = 0; i < n; i++) { cout << parent[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define m0(x) memset(x, 0, sizeof(x)) int dx4[4] = {1, 0, -1, 0}, dy4[4] = {0, 1, 0, -1}; int main() { int n, m; cin >> n >> m; // vector<int> a(n),b(n),parent(n),isroot(n),pcnt(n); vector<int> a(100100), b(100100), parent(100100), isroot(100100), pcnt(100100); vector<vector<int>> a2b(100100); for (int i = 0; i < n - 1 + m; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; a2b[a[i]].push_back(b[i]); isroot[b[i]] = -1; pcnt[b[i]]++; // oya nannnin iruka } int root; for (int i = 0; i < n; i++) { if (isroot[i] == 0) { root = i; parent[i] = 0; } else { parent[i] = -1; } } queue<int> qa; qa.push(root); while (qa.size() > 0) { int nowpar = qa.front(); qa.pop(); for (int i = 0; i < a2b[nowpar].size(); i++) { int nowchi = a2b[nowpar][i]; pcnt[nowchi]--; if (pcnt[nowchi] == 0) { parent[nowchi] = nowpar + 1; qa.push(nowchi); } } } for (int i = 0; i < n; i++) { cout << parent[i] << endl; } return 0; }
replace
17
19
17
20
0
p03142
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; vector<vector<int>> G(N + 1); vector<int> PC(N + 1, 0); for (int i = 0; i < N + M - 1; i++) { int a, b; cin >> a >> b; G[a].emplace_back(b); PC[b]++; } vector<int> P(N); queue<int> que; for (int i = 1; i < N + 1; i++) { if (PC[i] == 0) { que.push(i); P[i] = 0; break; } } while (!que.empty()) { int u = que.front(); que.pop(); for (int v : G[u]) { if (PC[v] > 1) { PC[v]--; } else { P[v] = u; que.push(v); } } } for (int i = 1; i < N + 1; i++) { cout << P[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; vector<vector<int>> G(N + 1); vector<int> PC(N + 1, 0); for (int i = 0; i < N + M - 1; i++) { int a, b; cin >> a >> b; G[a].emplace_back(b); PC[b]++; } vector<int> P(N + 1); queue<int> que; for (int i = 1; i < N + 1; i++) { if (PC[i] == 0) { que.push(i); P[i] = 0; break; } } while (!que.empty()) { int u = que.front(); que.pop(); for (int v : G[u]) { if (PC[v] > 1) { PC[v]--; } else { P[v] = u; que.push(v); } } } for (int i = 1; i < N + 1; i++) { cout << P[i] << '\n'; } return 0; }
replace
17
18
17
18
0
p03142
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define FOR(i, k, n) for (int i = (k); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) begin(x), end(x) using namespace std; using vecint = vector<int>; using ll = int64_t; void dfs(const vector<vecint> &g, int s, vector<int> &nodes) { for (int v : g[s]) { dfs(g, v, nodes); } for (int v : g[s]) { if (nodes[v] == -1) { nodes[v] = s; } } } int main() { int n, m; cin >> n >> m; vector<vecint> g(n); vecint d(n); REP(i, n + m - 1) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); ++d[b]; } int root = 0; REP(i, n) { if (d[i] == 0) root = i; } vecint nodes(n, -1); dfs(g, root, nodes); REP(i, n) { cout << nodes[i] + 1 << endl; } return 0; }
#include <bits/stdc++.h> #define FOR(i, k, n) for (int i = (k); i < (n); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) begin(x), end(x) using namespace std; using vecint = vector<int>; using ll = int64_t; void dfs(const vector<vecint> &g, int s, vector<int> &nodes) { for (int v : g[s]) { if (nodes[v] == -1) { dfs(g, v, nodes); } } for (int v : g[s]) { if (nodes[v] == -1) { nodes[v] = s; } } } int main() { int n, m; cin >> n >> m; vector<vecint> g(n); vecint d(n); REP(i, n + m - 1) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); ++d[b]; } int root = 0; REP(i, n) { if (d[i] == 0) root = i; } vecint nodes(n, -1); dfs(g, root, nodes); REP(i, n) { cout << nodes[i] + 1 << endl; } return 0; }
replace
11
12
11
14
TLE
p03142
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <set> #include <stack> #include <utility> #include <vector> using namespace std; // 頂点数、変数 int v, e; const int MAX_N = 10010; // 隣接リスト vector<int> g[MAX_N]; bool used[MAX_N]; // トポロジカルソートされた数列 vector<int> ans; void dfs(int u) { if (used[u]) return; used[u] = true; for (auto &i : g[u]) dfs(i); // 帰りがけ順で追加 ans.push_back(u); } void tsort() { for (int i = 0; i < v; ++i) dfs(i); reverse(ans.begin(), ans.end()); } int ans2[100100]; vector<int> G2[100100]; int order[100100]; int ansi(int i) { int maxorder = -1; for (auto p : G2[i]) { maxorder = max(maxorder, order[p]); } if (maxorder == -1) return 0; return ans[maxorder] + 1; } signed main(void) { cin >> v >> e; e += v - 1; for (int i = 0; i < e; ++i) { int s, t; cin >> s >> t; s--; t--; g[s].push_back(t); G2[t].push_back(s); } tsort(); for (int i = 0; i < v; ++i) { order[ans[i]] = i; } for (int i = 0; i < v; ++i) { cout << ansi(i) << endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <set> #include <stack> #include <utility> #include <vector> using namespace std; // 頂点数、変数 int v, e; const int MAX_N = 100010; // 隣接リスト vector<int> g[MAX_N]; bool used[MAX_N]; // トポロジカルソートされた数列 vector<int> ans; void dfs(int u) { if (used[u]) return; used[u] = true; for (auto &i : g[u]) dfs(i); // 帰りがけ順で追加 ans.push_back(u); } void tsort() { for (int i = 0; i < v; ++i) dfs(i); reverse(ans.begin(), ans.end()); } int ans2[100100]; vector<int> G2[100100]; int order[100100]; int ansi(int i) { int maxorder = -1; for (auto p : G2[i]) { maxorder = max(maxorder, order[p]); } if (maxorder == -1) return 0; return ans[maxorder] + 1; } signed main(void) { cin >> v >> e; e += v - 1; for (int i = 0; i < e; ++i) { int s, t; cin >> s >> t; s--; t--; g[s].push_back(t); G2[t].push_back(s); } tsort(); for (int i = 0; i < v; ++i) { order[ans[i]] = i; } for (int i = 0; i < v; ++i) { cout << ansi(i) << endl; } return 0; }
replace
13
14
13
14
0
p03142
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define Inf 0x3f3f3f3f using namespace std; typedef long long ll; inline ll min(ll x, ll y) { return x < y ? x : y; } inline ll max(ll x, ll y) { return x > y ? x : y; } const int Maxn = 1e5 + 5; int n, m, cnt, fir[Maxn], ind[Maxn], f[Maxn]; struct { int to, last; } e[Maxn]; void con(int u, int v) { e[++cnt].to = v; e[cnt].last = fir[u]; fir[u] = cnt; ind[v]++; } void BFS(int x) { queue<int> q; q.push(x); while (!q.empty()) { x = q.front(); q.pop(); for (int next = fir[x], nx; next; next = e[next].last) { nx = e[next].to; f[nx] = x; q.push(nx); } } } int main() { int a, b; cin >> n >> m; for (int i = 1; i < n + m; i++) { scanf("%d%d", &a, &b); con(a, b); } for (int i = 1; i <= n; i++) if (!ind[i]) { BFS(i); break; } for (int i = 1; i <= n; i++) printf("%d\n", f[i]); return 0; }
#include <bits/stdc++.h> #define Inf 0x3f3f3f3f using namespace std; typedef long long ll; inline ll min(ll x, ll y) { return x < y ? x : y; } inline ll max(ll x, ll y) { return x > y ? x : y; } const int Maxn = 1e5 + 5; int n, m, cnt, fir[Maxn], ind[Maxn], f[Maxn]; struct { int to, last; } e[Maxn]; void con(int u, int v) { e[++cnt].to = v; e[cnt].last = fir[u]; fir[u] = cnt; ind[v]++; } void BFS(int x) { queue<int> q; q.push(x); while (!q.empty()) { x = q.front(); q.pop(); for (int next = fir[x], nx; next; next = e[next].last) { nx = e[next].to; f[nx] = x; ind[nx]--; if (!ind[nx]) q.push(nx); } } } int main() { int a, b; cin >> n >> m; for (int i = 1; i < n + m; i++) { scanf("%d%d", &a, &b); con(a, b); } for (int i = 1; i <= n; i++) if (!ind[i]) { BFS(i); break; } for (int i = 1; i <= n; i++) printf("%d\n", f[i]); return 0; }
replace
26
27
26
29
TLE
p03142
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; #define p_ary(ary, a, b, i) \ do { \ cout << "["; \ for (int(i) = (a); (i) < (b); ++(i)) \ cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); \ cout << "]\n"; \ } while (0) #define p_map(map, it) \ do { \ cout << "{"; \ for (auto(it) = map.begin();; ++(it)) { \ if ((it) == map.end()) { \ cout << "}\n"; \ break; \ } else \ cout << "" << (it)->first << "=>" << (it)->second << ", "; \ } \ } while (0) vector<int> G[100010]; vector<int> l; void dfs(vector<bool> &used, int i) { used[i] = true; for (int j : G[i]) if (!used[j]) dfs(used, j); l.push_back(i); } int main() { int n, m, par; cin >> n >> m; vector<bool> used(10010, false), child(n, false); vector<int> a(n), ans(n, n); for (int i = 0; i < n + m - 1; ++i) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); child[v] = true; } for (int i = 0; i < n; ++i) if (!child[i]) par = i; dfs(used, par); a.push_back(n); for (int i = 0; i < n; ++i) a[l[i]] = i; for (int i = 0; i < n; ++i) for (int &j : G[i]) if (a[ans[j]] > a[i]) ans[j] = i; for (int i = 0; i < n; ++i) cout << (ans[i] + 1) % (n + 1) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; #define p_ary(ary, a, b, i) \ do { \ cout << "["; \ for (int(i) = (a); (i) < (b); ++(i)) \ cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); \ cout << "]\n"; \ } while (0) #define p_map(map, it) \ do { \ cout << "{"; \ for (auto(it) = map.begin();; ++(it)) { \ if ((it) == map.end()) { \ cout << "}\n"; \ break; \ } else \ cout << "" << (it)->first << "=>" << (it)->second << ", "; \ } \ } while (0) vector<int> G[100010]; vector<int> l; void dfs(vector<bool> &used, int i) { used[i] = true; for (int j : G[i]) if (!used[j]) dfs(used, j); l.push_back(i); } int main() { int n, m, par; cin >> n >> m; vector<bool> used(n, false), child(n, false); vector<int> a(n), ans(n, n); for (int i = 0; i < n + m - 1; ++i) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); child[v] = true; } for (int i = 0; i < n; ++i) if (!child[i]) par = i; dfs(used, par); a.push_back(n); for (int i = 0; i < n; ++i) a[l[i]] = i; for (int i = 0; i < n; ++i) for (int &j : G[i]) if (a[ans[j]] > a[i]) ans[j] = i; for (int i = 0; i < n; ++i) cout << (ans[i] + 1) % (n + 1) << "\n"; return 0; }
replace
39
40
39
40
0
p03142
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> typedef long long ll; using namespace std; typedef pair<int, int> P; struct TopologicalSort { int n; vector<set<int>> G; vector<bool> used; vector<int> indeg, p; TopologicalSort() {} TopologicalSort(int sz) : n(sz), G(n), used(n), indeg(n), p(0) {} void add_edge(int s, int t) { G[s].insert(t); } void bfs(int s) { queue<int> q; q.push(s); used[s] = 1; while (!q.empty()) { int v = q.front(); q.pop(); p.push_back(v); for (int u : G[v]) { indeg[u]--; if (indeg[u] == 0 && !used[u]) { used[u] = 1; q.push(u); } } } } vector<int> build() { fill(used.begin(), used.end(), 0); fill(indeg.begin(), indeg.end(), 0); for (int i = 0; i < n; i++) { for (int v : G[i]) { indeg[v]++; } } for (int i = 0; i < n; i++) { if (indeg[i] == 0 && !used[i]) { bfs(i); } } return p; } }; /////////////////////////////////////////////////////////////////////////////// vector<int> G[10005]; int main() { int N, M; cin >> N >> M; int a[N - 1 + M], b[N - 1 + M]; for (int i = 0; i < N - 1 + M; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; G[a[i]].push_back(b[i]); } TopologicalSort ts(N); for (int i = 0; i < N - 1 + M; i++) { ts.add_edge(a[i], b[i]); } vector<int> tsg = ts.build(); int ans[N]; fill(ans, ans + N, 0); for (int node : tsg) { for (int nxt : G[node]) { ans[nxt] = node + 1; } } for (int i = 0; i < N; i++) { cout << ans[i] << endl; } cout << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> typedef long long ll; using namespace std; typedef pair<int, int> P; struct TopologicalSort { int n; vector<set<int>> G; vector<bool> used; vector<int> indeg, p; TopologicalSort() {} TopologicalSort(int sz) : n(sz), G(n), used(n), indeg(n), p(0) {} void add_edge(int s, int t) { G[s].insert(t); } void bfs(int s) { queue<int> q; q.push(s); used[s] = 1; while (!q.empty()) { int v = q.front(); q.pop(); p.push_back(v); for (int u : G[v]) { indeg[u]--; if (indeg[u] == 0 && !used[u]) { used[u] = 1; q.push(u); } } } } vector<int> build() { fill(used.begin(), used.end(), 0); fill(indeg.begin(), indeg.end(), 0); for (int i = 0; i < n; i++) { for (int v : G[i]) { indeg[v]++; } } for (int i = 0; i < n; i++) { if (indeg[i] == 0 && !used[i]) { bfs(i); } } return p; } }; /////////////////////////////////////////////////////////////////////////////// vector<int> G[100005]; int main() { int N, M; cin >> N >> M; int a[N - 1 + M], b[N - 1 + M]; for (int i = 0; i < N - 1 + M; i++) { cin >> a[i] >> b[i]; a[i]--; b[i]--; G[a[i]].push_back(b[i]); } TopologicalSort ts(N); for (int i = 0; i < N - 1 + M; i++) { ts.add_edge(a[i], b[i]); } vector<int> tsg = ts.build(); int ans[N]; fill(ans, ans + N, 0); for (int node : tsg) { for (int nxt : G[node]) { ans[nxt] = node + 1; } } for (int i = 0; i < N; i++) { cout << ans[i] << endl; } cout << endl; return 0; }
replace
64
65
64
65
0
p03142
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ALL(a) (a).begin(), (a).end() #define sz(x) int(x.size()) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<long long, long long> Pll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<long long> vll; typedef vector<vector<long long>> vvll; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; const int INT_INF = 1 << 30; const double PI = acos(-1.0); #define MOD 1000000007LL #define endl "\n" int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, M; cin >> N >> M; vvll G(N, vll(0)); vll incount(N, 0); for (int i = 0; i < N - 1 + M; i++) { ll A, B; cin >> A >> B; A--; B--; G.at(A).push_back(B); incount.at(B)++; } int root; for (int i = 0; i < N; i++) { if (incount.at(i) == 0) { root = i; break; } } queue<int> que; que.push(root); vll num(N, -1); num.at(root) = 0; vll par(N, -1); par.at(root) = 0; while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < sz(G.at(v)); i++) { int next = G.at(v).at(i); if (num.at(v) + 1 > num.at(next)) { num.at(next) = num.at(v) + 1; par.at(next) = v + 1; que.push(next); } } } for (auto x : par) cout << x << endl; }
#include <bits/stdc++.h> #define ALL(a) (a).begin(), (a).end() #define sz(x) int(x.size()) using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<long long, long long> Pll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<long long> vll; typedef vector<vector<long long>> vvll; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; const int INT_INF = 1 << 30; const double PI = acos(-1.0); #define MOD 1000000007LL #define endl "\n" int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, M; cin >> N >> M; vvll G(N, vll(0)); vll incount(N, 0); for (int i = 0; i < N - 1 + M; i++) { ll A, B; cin >> A >> B; A--; B--; G.at(A).push_back(B); incount.at(B)++; } int root; for (int i = 0; i < N; i++) { if (incount.at(i) == 0) { root = i; break; } } queue<int> que; que.push(root); vll num(N, -1); num.at(root) = 0; vll par(N, -1); par.at(root) = 0; while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < sz(G.at(v)); i++) { int next = G.at(v).at(i); incount.at(next)--; if (incount.at(next) == 0) { num.at(next) = num.at(v) + 1; par.at(next) = v + 1; que.push(next); } } } for (auto x : par) cout << x << endl; }
replace
64
65
64
66
TLE
p03142
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define INF 1e9 #define MOD 1000000007 #define ll long long using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; typedef unsigned long long ull; typedef pair<ll, int> pr; vector<int> to[11111], from[11111]; bool used[11111]; int takasa[11111]; deque<int> tpsort; void dfs(int i) { if (used[i]) { return; } used[i] = true; for (auto &itr : to[i]) { dfs(itr); } tpsort.push_front(i); return; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; int nm = n + m - 1; int a, b; for (int i = 0; i < nm; i++) { cin >> a >> b; a--; b--; to[a].push_back(b); from[b].push_back(a); } for (int i = 0; i < n; i++) { dfs(i); } int t = 0; for (auto &itr : tpsort) { takasa[itr] = t; t++; } // for(int i=0;i<n;i++){ // cout<<takasa[i]<<" "; // } // cout<<endl; for (int i = 0; i < n; i++) { if (from[i].empty()) { cout << 0 << endl; // cout<<"emp"<<endl; continue; } int ans = from[i][0]; for (int j = 1; j < from[i].size(); j++) { if (takasa[from[i][j]] > takasa[ans]) { ans = from[i][j]; } } cout << ans + 1 << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define INF 1e9 #define MOD 1000000007 #define ll long long using namespace std; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; typedef unsigned long long ull; typedef pair<ll, int> pr; vector<int> to[111111], from[111111]; bool used[111111]; int takasa[111111]; deque<int> tpsort; void dfs(int i) { if (used[i]) { return; } used[i] = true; for (auto &itr : to[i]) { dfs(itr); } tpsort.push_front(i); return; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; int nm = n + m - 1; int a, b; for (int i = 0; i < nm; i++) { cin >> a >> b; a--; b--; to[a].push_back(b); from[b].push_back(a); } for (int i = 0; i < n; i++) { dfs(i); } int t = 0; for (auto &itr : tpsort) { takasa[itr] = t; t++; } // for(int i=0;i<n;i++){ // cout<<takasa[i]<<" "; // } // cout<<endl; for (int i = 0; i < n; i++) { if (from[i].empty()) { cout << 0 << endl; // cout<<"emp"<<endl; continue; } int ans = from[i][0]; for (int j = 1; j < from[i].size(); j++) { if (takasa[from[i][j]] > takasa[ans]) { ans = from[i][j]; } } cout << ans + 1 << endl; } return 0; }
replace
23
26
23
26
0
p03142
C++
Time Limit Exceeded
#include <list> #include <stdio.h> using namespace std; class Node { public: int parentsCount = 0; list<int> children; int setParents = 0; int trueParent = 0; int trueDepth = 0; void setDepth(int parent, int depth) { if (trueDepth < depth) { trueDepth = depth; trueParent = parent; } setParents++; } int getDepth() { if (parentsCount > setParents) { return -1; } return trueDepth; } }; int getRoot(Node *nodes, int size) { for (int i = 1; i < size; i++) { if (nodes[i].parentsCount == 0) { return i; } } return -1; } int main(void) { Node *nodes = NULL; int N, M, a, b; scanf("%d%d", &N, &M); nodes = new Node[N + 1]; for (int i = 0; i < N + M - 1; i++) { scanf("%d%d", &a, &b); nodes[b].parentsCount++; nodes[a].children.push_front(b); } list<int> list; list.push_front(getRoot(nodes, N + 1)); while (list.size() > 0) { int target = list.front(); list.pop_front(); int depth = nodes[target].getDepth(); for (const int x : nodes[target].children) { nodes[x].setDepth(target, depth + 1); if (nodes[x].getDepth() != -1) { list.push_front(x); } } } for (int i = 1; i < N + 1; i++) { printf("%d\n", nodes[i].trueParent); } }
#include <list> #include <stdio.h> using namespace std; class Node { public: int parentsCount = 0; list<int> children; int setParents = 0; int trueParent = 0; int trueDepth = 0; void setDepth(int parent, int depth) { if (trueDepth < depth) { trueDepth = depth; trueParent = parent; } setParents++; } int getDepth() { if (parentsCount > setParents) { return -1; } return trueDepth; } }; int getRoot(Node *nodes, int size) { for (int i = 1; i < size; i++) { if (nodes[i].parentsCount == 0) { return i; } } return -1; } int main(void) { Node *nodes = NULL; int N, M, a, b; scanf("%d%d", &N, &M); nodes = new Node[N + 1]; for (int i = 0; i < N + M - 1; i++) { scanf("%d%d", &a, &b); nodes[b].parentsCount++; nodes[a].children.push_front(b); } list<int> list; list.push_front(getRoot(nodes, N + 1)); while (list.size() > 0) { int target = list.front(); list.pop_front(); int depth = nodes[target].getDepth(); for (const int x : nodes[target].children) { nodes[x].setDepth(target, depth + 1); if (nodes[x].getDepth() != -1 && nodes[x].children.size() > 0) { list.push_front(x); } } } for (int i = 1; i < N + 1; i++) { printf("%d\n", nodes[i].trueParent); } }
replace
60
61
60
61
TLE
p03142
C++
Runtime Error
// #include "pch.h" // #include "stdafx.h" #include <algorithm> #include <cstring> #include <iostream> #include <math.h> #include <queue> #include <string> #include <vector> using namespace std; /*-----------------------------------------------------------------------------  ライブラリ -------------------------------------------------------------------------------*/ #if 0 // 3次元 vector<vector<vector<SDWORD>>> XXX(AAA, vector<vector<SDWORD>>(BBBB, vector<SDWORD>(CCC, -1))); #endif #if 0 // 降順ソート sort(getValue.rbegin(), getValue.rend()); #endif #if 0 // 2分探索 auto position = lower_bound(getValue.begin(), getValue.end(), 0); // 0以上の要素位置を探す int idx_lower = distance(getValue.begin(), position); // 0以上の要素インデックス int eraseNum = MIN(idx_lower, delCnt); // 要素位置が個数になる if (0 < eraseNum) { getValue.erase(getValue.begin(), getValue.begin() + eraseNum); } #endif /*-----------------------------------------------------------------------------  定義 -------------------------------------------------------------------------------*/ #define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i)) #define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i)) #define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i)) #define INF 2e9 #define MOD (1000 * 1000 * 1000 + 7) #define MIN(a, b) ((a) < (b) ? a : b) #define MAX(a, b) ((a) > (b) ? a : b) #define CeilN(x, n) (((((DWORD)(x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ \ */ #define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */ #define IsOdd(x) (((x)&0x01UL) == 0x01UL) #define IsEven(x) (!IsOdd((x))) #define ArrayLength(x) (sizeof(x) / sizeof(x[0])) #define ArrayEnd(a) (&(a)[ArrayLength(a)]) #define ArrayLast(a) (&(a)[ArrayLength(a) - 1]) #define MAX_BYTE (255) #define MIN_BYTE (0) #define MAX_WORD (65535) #define MIN_WORD (0) #define MAX_SWORD (32767) #define MIN_SWORD (-32768) #define MAX_DWORD (0xFFFFFFFF) #define MIN_DWORD (0) #define MAX_SDWORD ((SDWORD)0x7FFFFFFF) #define MIN_SDWORD ((SDWORD)0x80000000) #define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF) #define MIN_QWORD ((QWORD)0x0000000000000000) #define MAX_SQWORD ((SQWORD)0x7FFFFFFFFFFFFFFF) #define MIN_SQWORD ((SQWORD)0x8000000000000000) #define M_PI 3.14159265358979 #define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI) #define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360) #define BitSetV(Val, Bit) ((Val) |= (Bit)) #define BitClrV(Val, Bit) ((Val) &= (~(Bit))) #define BitTstV(Val, Bit) ((Val) & (Bit)) #define BitInvV(Val, Bit) ((Val) ^= (Bit)) #define BitTstVVal(Val, Bit) (BitTstV((Val), (Bit)) == (Bit)) typedef short SWORD; typedef long SDWORD; typedef long long SQWORD; typedef unsigned short WORD; typedef unsigned long DWORD; typedef unsigned long long int QWORD; typedef pair<int, int> P; /*-----------------------------------------------------------------------------  パラメータ定義 -------------------------------------------------------------------------------*/ #define N_MAX (10000) #define K_MAX (10) #define M_MAX (1000) #define H_MAX (1000) #define W_MAX (1000) /*-----------------------------------------------------------------------------  処理 -------------------------------------------------------------------------------*/ // メイン int main() { int N, M; static vector<int> G[N_MAX + 1]; static int jigen[N_MAX + 1]; // 入力 cin >> N >> M; REP(i, N + M - 1) { int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); jigen[b]++; } // 親探し int root = 0; REP(i, N + M - 1) { if (jigen[i] == 0) { root = i; break; } } // トポロジカルソート queue<int> que; static int parentList[N_MAX + 1]; que.push(root); parentList[root] = 0; while (!que.empty()) { int parent = que.front(); que.pop(); for (auto gOne : G[parent]) { jigen[gOne]--; if (jigen[gOne] == 0) { que.push(gOne); parentList[gOne] = parent + 1; } } } // リスト表示 REP(i, N) { cout << parentList[i] << endl; } cout << endl; return 0; }
// #include "pch.h" // #include "stdafx.h" #include <algorithm> #include <cstring> #include <iostream> #include <math.h> #include <queue> #include <string> #include <vector> using namespace std; /*-----------------------------------------------------------------------------  ライブラリ -------------------------------------------------------------------------------*/ #if 0 // 3次元 vector<vector<vector<SDWORD>>> XXX(AAA, vector<vector<SDWORD>>(BBBB, vector<SDWORD>(CCC, -1))); #endif #if 0 // 降順ソート sort(getValue.rbegin(), getValue.rend()); #endif #if 0 // 2分探索 auto position = lower_bound(getValue.begin(), getValue.end(), 0); // 0以上の要素位置を探す int idx_lower = distance(getValue.begin(), position); // 0以上の要素インデックス int eraseNum = MIN(idx_lower, delCnt); // 要素位置が個数になる if (0 < eraseNum) { getValue.erase(getValue.begin(), getValue.begin() + eraseNum); } #endif /*-----------------------------------------------------------------------------  定義 -------------------------------------------------------------------------------*/ #define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i)) #define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i)) #define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i)) #define INF 2e9 #define MOD (1000 * 1000 * 1000 + 7) #define MIN(a, b) ((a) < (b) ? a : b) #define MAX(a, b) ((a) > (b) ? a : b) #define CeilN(x, n) (((((DWORD)(x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ \ */ #define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */ #define IsOdd(x) (((x)&0x01UL) == 0x01UL) #define IsEven(x) (!IsOdd((x))) #define ArrayLength(x) (sizeof(x) / sizeof(x[0])) #define ArrayEnd(a) (&(a)[ArrayLength(a)]) #define ArrayLast(a) (&(a)[ArrayLength(a) - 1]) #define MAX_BYTE (255) #define MIN_BYTE (0) #define MAX_WORD (65535) #define MIN_WORD (0) #define MAX_SWORD (32767) #define MIN_SWORD (-32768) #define MAX_DWORD (0xFFFFFFFF) #define MIN_DWORD (0) #define MAX_SDWORD ((SDWORD)0x7FFFFFFF) #define MIN_SDWORD ((SDWORD)0x80000000) #define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF) #define MIN_QWORD ((QWORD)0x0000000000000000) #define MAX_SQWORD ((SQWORD)0x7FFFFFFFFFFFFFFF) #define MIN_SQWORD ((SQWORD)0x8000000000000000) #define M_PI 3.14159265358979 #define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI) #define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360) #define BitSetV(Val, Bit) ((Val) |= (Bit)) #define BitClrV(Val, Bit) ((Val) &= (~(Bit))) #define BitTstV(Val, Bit) ((Val) & (Bit)) #define BitInvV(Val, Bit) ((Val) ^= (Bit)) #define BitTstVVal(Val, Bit) (BitTstV((Val), (Bit)) == (Bit)) typedef short SWORD; typedef long SDWORD; typedef long long SQWORD; typedef unsigned short WORD; typedef unsigned long DWORD; typedef unsigned long long int QWORD; typedef pair<int, int> P; /*-----------------------------------------------------------------------------  パラメータ定義 -------------------------------------------------------------------------------*/ #define N_MAX (100000) #define K_MAX (10) #define M_MAX (1000) #define H_MAX (1000) #define W_MAX (1000) /*-----------------------------------------------------------------------------  処理 -------------------------------------------------------------------------------*/ // メイン int main() { int N, M; static vector<int> G[N_MAX + 1]; static int jigen[N_MAX + 1]; // 入力 cin >> N >> M; REP(i, N + M - 1) { int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); jigen[b]++; } // 親探し int root = 0; REP(i, N + M - 1) { if (jigen[i] == 0) { root = i; break; } } // トポロジカルソート queue<int> que; static int parentList[N_MAX + 1]; que.push(root); parentList[root] = 0; while (!que.empty()) { int parent = que.front(); que.pop(); for (auto gOne : G[parent]) { jigen[gOne]--; if (jigen[gOne] == 0) { que.push(gOne); parentList[gOne] = parent + 1; } } } // リスト表示 REP(i, N) { cout << parentList[i] << endl; } cout << endl; return 0; }
replace
91
92
91
92
0
p03142
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb emplace_back #define mp make_pair #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() constexpr int mod = 1e9 + 7; constexpr int32_t inf = 1001001001; constexpr int64_t infll = 1001001001001001001ll; constexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1}, dy[] = {-1, 0, 0, 1, -1, -1, 1, 1}; const string YES = "YES", NO = "NO", Yes = "Yes", No = "No", yes = "yes", no = "no"; // clang-format off // ostream &operator<<(ostream &os, __int128_t value) { if (ostream::sentry(os)) { __uint128_t tmp = value < 0 ? -value : value; char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; } // istream &operator>>(istream &is, __int128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } if (in[0] == '-') value *= -1; return is; } // ostream &operator<<(ostream &os, __uint128_t value) { if (ostream::sentry(os)) { char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[value % 10]; value /= 10; } while (value != 0); int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; } // istream &operator>>(istream &is, __uint128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << v[0]; for (int i = 1; i < v.size(); ++i) os << " " << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.fs << " " << p.sc; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.fs >> p.sc; return is; } struct IoSetup { IoSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; inline int64_t in() { int64_t x = 0; cin >> x; return x; } template <typename T> void sort(vector<T> &v) { sort(all(v)); } template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } // clang-format on signed main(int argc, char *argv[]) { int n = in(), m = in(); vector<int> deg(n, 0), w(n, 0); vector<vector<int>> G(n); vector<int> a(n + m), b(n + m); vector<int> ans(n), dep(n, -1); for (int i = 0; i < n - 1 + m; ++i) { a[i] = in() - 1; b[i] = in() - 1; G[a[i]].pb(b[i]); deg[b[i]]++; } int p = -1; for (int i = 0; i < n; ++i) { if (deg[i] == 0) p = i; } int cnt = 0; vector<int> ts(n, -1); stack<int> st; st.push(p); while (!st.empty()) { int u = st.top(); st.pop(); ts[u] = cnt++; for (auto &to : G[u]) { deg[to]--; if (deg[to] == 0) st.push(to); } } for (int i = 0; i < n - 1 + m; ++i) { if (chmax(dep[i], ts[a[i]])) { ans[b[i]] = a[i] + 1; } } for (int i = 0; i < n; ++i) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb emplace_back #define mp make_pair #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() constexpr int mod = 1e9 + 7; constexpr int32_t inf = 1001001001; constexpr int64_t infll = 1001001001001001001ll; constexpr int dx[] = {0, -1, 1, 0, -1, 1, -1, 1}, dy[] = {-1, 0, 0, 1, -1, -1, 1, 1}; const string YES = "YES", NO = "NO", Yes = "Yes", No = "No", yes = "yes", no = "no"; // clang-format off // ostream &operator<<(ostream &os, __int128_t value) { if (ostream::sentry(os)) { __uint128_t tmp = value < 0 ? -value : value; char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; } // istream &operator>>(istream &is, __int128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } if (in[0] == '-') value *= -1; return is; } // ostream &operator<<(ostream &os, __uint128_t value) { if (ostream::sentry(os)) { char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[value % 10]; value /= 10; } while (value != 0); int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; } // istream &operator>>(istream &is, __uint128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << v[0]; for (int i = 1; i < v.size(); ++i) os << " " << v[i]; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.fs << " " << p.sc; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.fs >> p.sc; return is; } struct IoSetup { IoSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; inline int64_t in() { int64_t x = 0; cin >> x; return x; } template <typename T> void sort(vector<T> &v) { sort(all(v)); } template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } // clang-format on signed main(int argc, char *argv[]) { int n = in(), m = in(); vector<int> deg(n, 0), w(n, 0); vector<vector<int>> G(n); vector<int> a(n + m), b(n + m); vector<int> ans(n), dep(n, -1); for (int i = 0; i < n - 1 + m; ++i) { a[i] = in() - 1; b[i] = in() - 1; G[a[i]].pb(b[i]); deg[b[i]]++; } int p = -1; for (int i = 0; i < n; ++i) { if (deg[i] == 0) p = i; } int cnt = 0; vector<int> ts(n, -1); stack<int> st; st.push(p); while (!st.empty()) { int u = st.top(); st.pop(); ts[u] = cnt++; for (auto &to : G[u]) { deg[to]--; if (deg[to] == 0) st.push(to); } } for (int i = 0; i < n - 1 + m; ++i) { if (chmax(dep[b[i]], ts[a[i]])) { ans[b[i]] = a[i] + 1; } } for (int i = 0; i < n; ++i) { cout << ans[i] << endl; } }
replace
80
81
80
81
0
p03142
C++
Time Limit Exceeded
/* これを入れて実行 g++ code.cpp ./a.out */ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 60; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) { return f.second > s.second; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll conbinationMemo[100][100]; void cmemoInit() { rep(i, 100) { rep(j, 100) { conbinationMemo[i][j] = -1; } } } ll nCr(ll n, ll r) { if (conbinationMemo[n][r] != -1) return conbinationMemo[n][r]; if (r == 0 || r == n) { return 1; } else if (r == 1) { return n; } return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r) { r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- ll n, m; vector<vector<ll>> g; vector<vector<ll>> rg; vector<ll> t; void dfs(ll now, vector<int> &visited) { visited[now] = 1; rep(i, g[now].size()) { ll next = g[now][i]; dfs(next, visited); } t.push_back(now); } int main(void) { cin >> n >> m; g.resize(n); rg.resize(n); vector<int> appeared(n, 0); rep(i, n - 1 + m) { ll a, b; cin >> a >> b; a--, b--; g[a].push_back(b); rg[b].push_back(a); appeared[b] = 1; } ll root = 0; rep(i, n) { if (!appeared[i]) { root = i; break; } } vector<int> visited(n, 0); dfs(root, visited); reverse(all(t)); map<ll, ll> mp; rep(i, t.size()) { mp[t[i]] = i; } rep(i, n) { sort(all(rg[i]), [&mp](const ll &f, const ll &s) { return mp[f] > mp[s]; }); } vector<ll> ans(n); rep(i, n) { if (rg[i].size() == 0) { ans[i] = 0; } else { ans[i] = rg[i][0] + 1; } } rep(i, ans.size()) { cout << ans[i] << endl; } }
/* これを入れて実行 g++ code.cpp ./a.out */ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <vector> #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 60; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) { return f.second > s.second; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll conbinationMemo[100][100]; void cmemoInit() { rep(i, 100) { rep(j, 100) { conbinationMemo[i][j] = -1; } } } ll nCr(ll n, ll r) { if (conbinationMemo[n][r] != -1) return conbinationMemo[n][r]; if (r == 0 || r == n) { return 1; } else if (r == 1) { return n; } return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r) { r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- ll n, m; vector<vector<ll>> g; vector<vector<ll>> rg; vector<ll> t; void dfs(ll now, vector<int> &visited) { visited[now] = 1; rep(i, g[now].size()) { ll next = g[now][i]; if (visited[next]) continue; dfs(next, visited); } t.push_back(now); } int main(void) { cin >> n >> m; g.resize(n); rg.resize(n); vector<int> appeared(n, 0); rep(i, n - 1 + m) { ll a, b; cin >> a >> b; a--, b--; g[a].push_back(b); rg[b].push_back(a); appeared[b] = 1; } ll root = 0; rep(i, n) { if (!appeared[i]) { root = i; break; } } vector<int> visited(n, 0); dfs(root, visited); reverse(all(t)); map<ll, ll> mp; rep(i, t.size()) { mp[t[i]] = i; } rep(i, n) { sort(all(rg[i]), [&mp](const ll &f, const ll &s) { return mp[f] > mp[s]; }); } vector<ll> ans(n); rep(i, n) { if (rg[i].size() == 0) { ans[i] = 0; } else { ans[i] = rg[i][0] + 1; } } rep(i, ans.size()) { cout << ans[i] << endl; } }
insert
89
89
89
91
TLE
p03142
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10; const ll LINF = 1e18; using namespace std; #define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++) #define rrep(i, a, b) for (ll i = (a); i < (b); i++) #define rrrep(i, a, b) for (ll i = (a); i >= (b); i--) #define all(v) (v).begin(), (v).end() #define Size(n) (n).size() #define Cout(x) cout << (x) << endl #define doublecout(a) cout << fixed << setprecision(15) << a << endl; #define Cerr(x) cerr << (x) << endl #define fi first #define se second #define P pair<ll, ll> #define m_p make_pair #define V vector<ll> #define U_MAP unordered_map<ll, ll> #define pq priority_queue<ll> #define rpq priority_queue<ll, vector<ll>, greater<ll>> #define p_b push_back ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k, q; ld doua; int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; // int dy[]={-1,0,1,-1,1,-1,0,1}; // int dx[]={-1,-1,-1,0,0,1,1,1}; string alph("abcdefghijklmnopqrstuvwxyz"), s; bool fl; struct edge { int to, cost; }; V g[ohara]; ll nene[ohara], ne, out[ohara]; ll dp[ohara]; V kae[ohara]; //------ Believe yourself as a genius!!!!!! ------ int main(void) { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, n + m - 1) { cin >> a[i] >> b[i]; a[i]--; b[i]--; g[a[i]].p_b(b[i]); nene[b[i]]++; } rep(i, n) { if (nene[i] == 0) { ne = i; break; } } queue<ll> que; que.push(ne); while (1) { if (que.empty()) break; ll moto = que.front(); que.pop(); rep(i, Size(g[moto])) { ll to = g[moto][i]; que.push(to); dp[to] = max(dp[to], dp[moto] + 1); } } rep(i, n + m - 1) { if (dp[a[i]] + 1 == dp[b[i]]) continue; out[i] = 1; } rep(i, n + m - 1) { if (out[i]) continue; kae[b[i]].p_b(a[i]); } rep(i, n) { if (i == ne) Cout(0); else Cout(kae[i][0] + 1); } return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10; const ll LINF = 1e18; using namespace std; #define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++) #define rrep(i, a, b) for (ll i = (a); i < (b); i++) #define rrrep(i, a, b) for (ll i = (a); i >= (b); i--) #define all(v) (v).begin(), (v).end() #define Size(n) (n).size() #define Cout(x) cout << (x) << endl #define doublecout(a) cout << fixed << setprecision(15) << a << endl; #define Cerr(x) cerr << (x) << endl #define fi first #define se second #define P pair<ll, ll> #define m_p make_pair #define V vector<ll> #define U_MAP unordered_map<ll, ll> #define pq priority_queue<ll> #define rpq priority_queue<ll, vector<ll>, greater<ll>> #define p_b push_back ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k, q; ld doua; int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; // int dy[]={-1,0,1,-1,1,-1,0,1}; // int dx[]={-1,-1,-1,0,0,1,1,1}; string alph("abcdefghijklmnopqrstuvwxyz"), s; bool fl; struct edge { int to, cost; }; V g[ohara]; ll nene[ohara], ne, out[ohara]; ll dp[ohara]; V kae[ohara]; //------ Believe yourself as a genius!!!!!! ------ int main(void) { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, n + m - 1) { cin >> a[i] >> b[i]; a[i]--; b[i]--; g[a[i]].p_b(b[i]); nene[b[i]]++; } rep(i, n) { if (nene[i] == 0) { ne = i; break; } } queue<ll> que; que.push(ne); while (1) { if (que.empty()) break; ll moto = que.front(); que.pop(); rep(i, Size(g[moto])) { ll to = g[moto][i]; nene[to]--; if (nene[to] == 0) que.push(to); dp[to] = max(dp[to], dp[moto] + 1); } } rep(i, n + m - 1) { if (dp[a[i]] + 1 == dp[b[i]]) continue; out[i] = 1; } rep(i, n + m - 1) { if (out[i]) continue; kae[b[i]].p_b(a[i]); } rep(i, n) { if (i == ne) Cout(0); else Cout(kae[i][0] + 1); } return 0; }
replace
72
73
72
75
TLE
p03142
C++
Runtime Error
#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <math.h> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; using pii = pair<int, int>; using ll = long long; using ld = long double; #define pb push_back #define mp make_pair #define sc second #define fr first #define stpr setprecision #define cYES cout << "YES" << endl #define cNO cout << "NO" << endl #define cYes cout << "Yes" << endl #define cNo cout << "No" << endl #define rep(i, n) for (ll i = 0; i < (n); ++i) #define Rep(i, a, b) for (ll i = (a); i < (b); ++i) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define rRep(i, a, b) for (int i = a; i >= b; i--) #define crep(i) for (char i = 'a'; i <= 'z'; ++i) #define psortsecond(A, N) \ sort(A, A + N, \ [](const pii &a, const pii &b) { return a.second < b.second; }); #define ALL(x) (x).begin(), (x).end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) { \ cout << x << ' '; \ } \ cout << endl; #define endl '\n' int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } constexpr ll MOD = 1000000007; constexpr ll INF = 1000000011; constexpr ll MOD2 = 998244353; constexpr ll LINF = 1001002003004005006ll; constexpr ld EPS = 10e-8; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &&x : v) is >> x; return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first; is >> p.second; return is; } template <typename T, typename U> ostream &operator>>(ostream &os, const pair<T, U> &p) { os << p.first << ' ' << p.second; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) { if (i != begin(v)) os << ' '; os << *i; } return os; } vector<vector<ll>> E(10007); // 各点の持つ子の情報 ll color[10007]; // 訪れ所の bool B[100007]; // outに入ってるか否か list<ll> out; // トポロジカルソート済みの配列を入れる ll N, M, a, b; // N:頂点数 M:エッジ数 ll indeg[100007]; // 各点の入次数。訪問した回数だけ減っていく。 ll parent[100007]; // 根付き木とみなした時の親 void bfs(ll s) { queue<ll> Q; Q.push(s); B[s] = true; while (!Q.empty()) { ll u = Q.front(); Q.pop(); out.pb(u); rep(i, E[u].size()) { ll v = E[u][i]; indeg[v]--; if (indeg[v] == 0 && B[v] == false) { parent[v] = u; B[v] = true; Q.push(v); } } } } void topological_sort() { rep(i, N) { // 初期化 indeg[i] = 0; parent[i] = 0; } rep(i, N) { rep(j, E[i].size()) { // 入次数を持たせる indeg[E[i][j]]++; } } rep(i, N) { if (indeg[i] == 0 && !B[i]) { parent[i] = -1; bfs(i); } } } int main() { cin >> N >> M; ll A[100007], B[100007], D[100007]; rep(i, N) { B[i] = false; } M = M + N - 1; rep(i, M) { cin >> a >> b; a--; b--; E[a].pb(b); } topological_sort(); ll T = 0; rep(i, N) { cout << parent[i] + 1 << endl; } }
#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <math.h> #include <queue> #include <regex> #include <set> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; using pii = pair<int, int>; using ll = long long; using ld = long double; #define pb push_back #define mp make_pair #define sc second #define fr first #define stpr setprecision #define cYES cout << "YES" << endl #define cNO cout << "NO" << endl #define cYes cout << "Yes" << endl #define cNo cout << "No" << endl #define rep(i, n) for (ll i = 0; i < (n); ++i) #define Rep(i, a, b) for (ll i = (a); i < (b); ++i) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define rRep(i, a, b) for (int i = a; i >= b; i--) #define crep(i) for (char i = 'a'; i <= 'z'; ++i) #define psortsecond(A, N) \ sort(A, A + N, \ [](const pii &a, const pii &b) { return a.second < b.second; }); #define ALL(x) (x).begin(), (x).end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) { \ cout << x << ' '; \ } \ cout << endl; #define endl '\n' int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } constexpr ll MOD = 1000000007; constexpr ll INF = 1000000011; constexpr ll MOD2 = 998244353; constexpr ll LINF = 1001002003004005006ll; constexpr ld EPS = 10e-8; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &&x : v) is >> x; return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first; is >> p.second; return is; } template <typename T, typename U> ostream &operator>>(ostream &os, const pair<T, U> &p) { os << p.first << ' ' << p.second; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) { if (i != begin(v)) os << ' '; os << *i; } return os; } vector<vector<ll>> E(100007); // 各点の持つ子の情報 ll color[100007]; // 訪れ所の bool B[100007]; // outに入ってるか否か list<ll> out; // トポロジカルソート済みの配列を入れる ll N, M, a, b; // N:頂点数 M:エッジ数 ll indeg[100007]; // 各点の入次数。訪問した回数だけ減っていく。 ll parent[100007]; // 根付き木とみなした時の親 void bfs(ll s) { queue<ll> Q; Q.push(s); B[s] = true; while (!Q.empty()) { ll u = Q.front(); Q.pop(); out.pb(u); rep(i, E[u].size()) { ll v = E[u][i]; indeg[v]--; if (indeg[v] == 0 && B[v] == false) { parent[v] = u; B[v] = true; Q.push(v); } } } } void topological_sort() { rep(i, N) { // 初期化 indeg[i] = 0; parent[i] = 0; } rep(i, N) { rep(j, E[i].size()) { // 入次数を持たせる indeg[E[i][j]]++; } } rep(i, N) { if (indeg[i] == 0 && !B[i]) { parent[i] = -1; bfs(i); } } } int main() { cin >> N >> M; ll A[100007], B[100007], D[100007]; rep(i, N) { B[i] = false; } M = M + N - 1; rep(i, M) { cin >> a >> b; a--; b--; E[a].pb(b); } topological_sort(); ll T = 0; rep(i, N) { cout << parent[i] + 1 << endl; } }
replace
96
99
96
99
0
p03142
C++
Runtime Error
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define pll pair<ll, ll> #define pq priority_queue #define pb push_back #define eb emplace_back #define fi first #define se second #define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define lb(c, x) distance(c.begin(), lower_bound(all(c), x)) #define ub(c, x) distance(c.begin(), upper_bound(all(c), x)) using namespace std; 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 ll INF = 1e9 + 7; int main() { ll n, m; cin >> n >> m; vector<set<ll>> from(n); vector<set<ll>> to(n); rep(i, n + m - 1) { ll a, b; cin >> a >> b; a--; b--; from[b].insert(a); to[a].insert(b); } priority_queue<pll> PQ; vector<ll> ans(n); rep(i, n) { PQ.push({n - from[i].size(), i}); } ll p = 0; pll st = PQ.top(); ans[st.fi] = 0; ll cnt = 1; while (cnt < n) { pll x = PQ.top(); PQ.pop(); p = x.fi; for (auto c : to[x.se]) { if (from[c].size() == 1) { ans[c] = x.se + 1; cnt++; PQ.push({n, c}); } from[c].erase(x.se); } } rep(i, n) { cout << ans[i] << endl; } }
#pragma GCC optimize("O3") #include <bits/stdc++.h> #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define pll pair<ll, ll> #define pq priority_queue #define pb push_back #define eb emplace_back #define fi first #define se second #define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define lb(c, x) distance(c.begin(), lower_bound(all(c), x)) #define ub(c, x) distance(c.begin(), upper_bound(all(c), x)) using namespace std; 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 ll INF = 1e9 + 7; int main() { ll n, m; cin >> n >> m; vector<set<ll>> from(n); vector<set<ll>> to(n); rep(i, n + m - 1) { ll a, b; cin >> a >> b; a--; b--; from[b].insert(a); to[a].insert(b); } priority_queue<pll> PQ; vector<ll> ans(n); rep(i, n) { PQ.push({n - from[i].size(), i}); } ll p = 0; pll st = PQ.top(); ans[st.se] = 0; ll cnt = 1; while (cnt < n) { pll x = PQ.top(); PQ.pop(); p = x.fi; for (auto c : to[x.se]) { if (from[c].size() == 1) { ans[c] = x.se + 1; cnt++; PQ.push({n, c}); } from[c].erase(x.se); } } rep(i, n) { cout << ans[i] << endl; } }
replace
51
52
51
52
0
p03142
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; int N, M; vector<int> pathes[100500]; vector<int> inv[100050]; int parent[100050]; int Rank[100050]; void bfs(int from) { if (inv[from].empty()) { Rank[from] = 1; parent[from] = 0; return; } int par = 0; int now = 0; for (int i = 0; i < inv[from].size(); i++) { int from_from = inv[from][i]; bfs(from_from); if (now < Rank[from_from]) { now = Rank[from_from]; par = from_from; } } parent[from] = par; Rank[from] = now + 1; } int main() { // cout.precision(10); cin >> N >> M; for (int i = 1; i <= N - 1 + M; i++) { int a, b; cin >> a >> b; pathes[a].push_back(b); inv[b].push_back(a); } for (int i = 1; i <= N; i++) { bfs(i); } for (int i = 1; i <= N; i++) { cout << parent[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; int N, M; vector<int> pathes[100500]; vector<int> inv[100050]; int parent[100050]; int Rank[100050]; void bfs(int from) { if (inv[from].empty()) { Rank[from] = 1; parent[from] = 0; return; } if (parent[from] != 0) return; int par = 0; int now = 0; for (int i = 0; i < inv[from].size(); i++) { int from_from = inv[from][i]; bfs(from_from); if (now < Rank[from_from]) { now = Rank[from_from]; par = from_from; } } parent[from] = par; Rank[from] = now + 1; } int main() { // cout.precision(10); cin >> N >> M; for (int i = 1; i <= N - 1 + M; i++) { int a, b; cin >> a >> b; pathes[a].push_back(b); inv[b].push_back(a); } for (int i = 1; i <= N; i++) { bfs(i); } for (int i = 1; i <= N; i++) { cout << parent[i] << endl; } return 0; }
insert
20
20
20
22
TLE
p03142
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 rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, a, b) for (ll i = a; i < b; ++i) const ll MOD = 1e9 + 7; const ll INF = 1e9; const ll IINF = 1e18; const double EPS = 1e-8; const double pi = acos(-1); 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; } int main() { int N, M; cin >> N >> M; vector<vector<int>> G(N); vector<int> cnt(N); rep(i, N - 1 + M) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); ++cnt[b]; } int root; rep(i, N) if (cnt[i] == 0) root = i; vector<int> par(N, -1); queue<int> que; que.push(root); while (!que.empty()) { int v = que.front(); que.pop(); for (auto nv : G[v]) { que.push(nv); par[nv] = v; } } rep(i, N) cout << par[i] + 1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, a, b) for (ll i = a; i < b; ++i) const ll MOD = 1e9 + 7; const ll INF = 1e9; const ll IINF = 1e18; const double EPS = 1e-8; const double pi = acos(-1); 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; } int main() { int N, M; cin >> N >> M; vector<vector<int>> G(N); vector<int> cnt(N); rep(i, N - 1 + M) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); ++cnt[b]; } int root; rep(i, N) if (cnt[i] == 0) root = i; vector<int> par(N, -1); queue<int> que; que.push(root); while (!que.empty()) { int v = que.front(); que.pop(); for (auto nv : G[v]) { --cnt[nv]; if (cnt[nv] > 0) continue; que.push(nv); par[nv] = v; } } rep(i, N) cout << par[i] + 1 << endl; }
insert
49
49
49
52
TLE
p03142
Python
Time Limit Exceeded
n, m = map(int, input().split()) g = [[] for _ in range(n)] inv = [0] * n for _ in range(n - 1 + m): a, b = map(int, input().split()) g[a - 1].append(b - 1) inv[b - 1] += 1 for i in range(n): if inv[i] == 0: root = i break d = [0] * n s = [root] while s: p = s.pop() for node in g[p]: d[node] = max(d[node], d[p] + 1) s.append(node) ans = [0] * n for i in range(n): for node in g[i]: if d[node] == d[i] + 1: ans[node] = i + 1 for x in ans: print(x)
n, m = map(int, input().split()) g = [[] for _ in range(n)] inv = [0] * n for _ in range(n - 1 + m): a, b = map(int, input().split()) g[a - 1].append(b - 1) inv[b - 1] += 1 for i in range(n): if inv[i] == 0: root = i break d = [0] * n s = [root] while s: p = s.pop() for node in g[p]: inv[node] -= 1 if inv[node] == 0: d[node] = max(d[node], d[p] + 1) s.append(node) ans = [0] * n for i in range(n): for node in g[i]: if d[node] == d[i] + 1: ans[node] = i + 1 for x in ans: print(x)
replace
16
18
16
20
TLE
p03142
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define REP(i, n) for (int(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; // priority_queue<ll> max;//大きい順 // priority_queue<ll, Array, greater<ll>> min;//小さい順 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; } // sortは初期で昇順 greater<hoge>()で降順 // substr 文字列取り出し // upper_bound // ある値より大きい一番左のイテレータを返す、lowerは以上(setに対して使うとO(N)なので、setのメンバ関数を使う // stoi 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() - 2) { REP(i, G.size()) { REP(j, G[i].size()) { if (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() - 2) { REP(i, G.size()) { REP(j, G[i].size()) { if (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)); } } } } 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]); } } } } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unionSet(int x, int y) { // xと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 -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; 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); } // 場所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); } }; // 約数求める //約数 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()); } 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(), 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; } ll Pow(ll x, ll n) { ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; 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; } 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]); } } }; 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 used2[10010]; int main() { ll n, m; cin >> n >> m; Graph graph(n), graph2(n); REP(i, m + n - 1) { ll a, b; cin >> a >> b; a--; b--; add_edge(graph, b, a, 1, false, 0); add_edge(graph2, a, b, 1, false, 0); used2[b]++; } queue<ll> que; map<ll, ll> ma; REP(i, n) { if (used2[i] == 0) { que.push(i); ma[i] = -1; } } while (que.size()) { REP(i, graph2[que.front()].size()) { used2[graph2[que.front()][i].to]--; if (used2[graph2[que.front()][i].to] == 0) { ma[graph2[que.front()][i].to] = que.front(); que.push(graph2[que.front()][i].to); } } que.pop(); } for (auto itr : ma) { cout << itr.second + 1 << endl; } return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; #define MOD 1000000007 #define REP(i, n) for (int(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; // priority_queue<ll> max;//大きい順 // priority_queue<ll, Array, greater<ll>> min;//小さい順 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; } // sortは初期で昇順 greater<hoge>()で降順 // substr 文字列取り出し // upper_bound // ある値より大きい一番左のイテレータを返す、lowerは以上(setに対して使うとO(N)なので、setのメンバ関数を使う // stoi 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() - 2) { REP(i, G.size()) { REP(j, G[i].size()) { if (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() - 2) { REP(i, G.size()) { REP(j, G[i].size()) { if (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)); } } } } 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]); } } } } class UnionFind { vector<int> data; ll num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unionSet(int x, int y) { // xと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 -= (x != y); return x != y; } bool findSet(int x, int y) { // xとyが同じ集合か返す return root(x) == root(y); } int root(int x) { // xのルートを返す return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { // xの集合のサイズを返す return -data[root(x)]; } int numSet() { // 集合の数を返す return num; } }; 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); } // 場所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); } }; // 約数求める //約数 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()); } 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(), 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; } ll Pow(ll x, ll n) { ll res = 1LL; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } // nCrとか class Combination { public: Array fact; Array inv; ll mod; ll mod_inv(ll x) { ll n = mod - 2LL; 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; } 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]); } } }; 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 used2[100010]; int main() { ll n, m; cin >> n >> m; Graph graph(n), graph2(n); REP(i, m + n - 1) { ll a, b; cin >> a >> b; a--; b--; add_edge(graph, b, a, 1, false, 0); add_edge(graph2, a, b, 1, false, 0); used2[b]++; } queue<ll> que; map<ll, ll> ma; REP(i, n) { if (used2[i] == 0) { que.push(i); ma[i] = -1; } } while (que.size()) { REP(i, graph2[que.front()].size()) { used2[graph2[que.front()][i].to]--; if (used2[graph2[que.front()][i].to] == 0) { ma[graph2[que.front()][i].to] = que.front(); que.push(graph2[que.front()][i].to); } } que.pop(); } for (auto itr : ma) { cout << itr.second + 1 << endl; } return 0; }
replace
309
310
309
310
0
p03142
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) typedef long long ll; typedef long double ld; typedef pair<int, int> P; int main() { int n, m; cin >> n >> m; vector<vector<int>> g(n); vector<int> deg(n, 0); rep(i, n + m - 1) { int a, b; cin >> a >> b; a--; b--; g[a].emplace_back(b); deg[b]++; } int root = -1; for (int i = 0; i < n; i++) if (!deg[i]) root = i; vector<int> par(n, 0); queue<int> que; que.push(root); par[root] = -1; while (!que.empty()) { int u = que.front(); que.pop(); for (auto v : g[u]) { par[v] = u; que.push(v); } } for (int i = 0; i < n; i++) cout << par[i] + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) typedef long long ll; typedef long double ld; typedef pair<int, int> P; int main() { int n, m; cin >> n >> m; vector<vector<int>> g(n); vector<int> deg(n, 0); rep(i, n + m - 1) { int a, b; cin >> a >> b; a--; b--; g[a].emplace_back(b); deg[b]++; } int root = -1; for (int i = 0; i < n; i++) if (!deg[i]) root = i; vector<int> par(n, 0); queue<int> que; que.push(root); par[root] = -1; while (!que.empty()) { int u = que.front(); que.pop(); for (auto v : g[u]) { deg[v]--; if (deg[v] == 0) { que.push(v); par[v] = u; } } } for (int i = 0; i < n; i++) cout << par[i] + 1 << endl; return 0; }
replace
33
35
33
38
TLE
p03142
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define Rep(i, a, b) for (int i = a; i < b; i++) #define REP(i, a, b) for (int i = a; i <= b; i++) #define rev(i, n) for (int i = n - 1; i >= 0; i--) #define vi vector<int> #define pb push_back #define pi pair<int, int> #define vp vector<pair<int, int>> #define mp make_pair #define all(v) (v).begin(), (v).end() #define fi first #define se second #define INF 1000000000 #define eps 1e-7 #define mod 1000000007 #define int ll using namespace std; int P[100000 + 10]; vi adj[10000 + 10]; int in_deg[10000 + 10]; signed main(void) { int N, M; cin >> N >> M; REP(i, 1, N) P[i] = i; rep(i, N - 1 + M) { int A, B; cin >> A >> B; in_deg[B]++; adj[A].push_back(B); } /* breadth first search */ queue<int> Q; REP(i, 1, N) if (in_deg[i] == 0) Q.push(i); while (!Q.empty()) { int top = Q.front(); Q.pop(); for (auto node : adj[top]) { in_deg[node]--; if (in_deg[node] == 0) { Q.push(node); P[node] = top; } } } REP(i, 1, N) { if (P[i] == i) cout << 0 << endl; else cout << P[i] << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define Rep(i, a, b) for (int i = a; i < b; i++) #define REP(i, a, b) for (int i = a; i <= b; i++) #define rev(i, n) for (int i = n - 1; i >= 0; i--) #define vi vector<int> #define pb push_back #define pi pair<int, int> #define vp vector<pair<int, int>> #define mp make_pair #define all(v) (v).begin(), (v).end() #define fi first #define se second #define INF 1000000000 #define eps 1e-7 #define mod 1000000007 #define int ll using namespace std; int P[100000 + 10]; vi adj[100000 + 10]; int in_deg[100000 + 10]; signed main(void) { int N, M; cin >> N >> M; REP(i, 1, N) P[i] = i; rep(i, N - 1 + M) { int A, B; cin >> A >> B; in_deg[B]++; adj[A].push_back(B); } /* breadth first search */ queue<int> Q; REP(i, 1, N) if (in_deg[i] == 0) Q.push(i); while (!Q.empty()) { int top = Q.front(); Q.pop(); for (auto node : adj[top]) { in_deg[node]--; if (in_deg[node] == 0) { Q.push(node); P[node] = top; } } } REP(i, 1, N) { if (P[i] == i) cout << 0 << endl; else cout << P[i] << endl; } return 0; }
replace
21
23
21
23
0
p03142
C++
Time Limit Exceeded
// #include <bits/stdc++.h> #include <algorithm> #include <iostream> #include <array> #include <bitset> #include <complex> #include <cstring> #include <deque> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <numeric> #include <random> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ull; // typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<double, int> pdi; typedef pair<long double, int> pdbi; typedef pair<int, pii> pipii; typedef pair<ll, pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1 << 30; const ll INFLL = 1LL << 62; const long double EPS = 1e-12; const ll mod = 1000000000 + 7; // const ll mod = 998244353; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) \ (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false // debug #define DEBUG #define DUMPOUT cout #ifdef DEBUG #define dump(...) \ DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() { DUMPOUT << endl; }; template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } // ostream template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "["; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int n, m; int a[100000 + 5], b[100000 + 5]; vector<vector<int>> g(100000 + 5); vector<int> ans(100000 + 5, -1); vector<int> indeg(100000 + 5), outdeg(100000 + 5); vector<bool> visited(100000 + 5); void dfs(int now, int par) { visited[now] = true; if (par != -1 && ans[now] == -1) { ans[now] = par + 1; } for (auto nx : g[now]) { if (nx == par || visited[nx]) continue; if (indeg[nx] == 1) { for (auto nx2 : g[now]) { indeg[nx2]--; } dfs(nx, now); for (auto nx2 : g[now]) { indeg[nx2]++; } } } return; } void bfs(int r, int par) { queue<pii> q; q.push({r, par}); while (!q.empty()) { pii p = q.front(); q.pop(); int now = p.fi; int pa = p.se; visited[now] = true; if (now != r) ans[now] = pa + 1; for (auto nx : g[now]) { if (indeg[nx] == 1 && !visited[nx]) { visited[nx] = true; q.push({nx, now}); } } for (auto nx : g[now]) { indeg[nx]--; } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, n - 1 + m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; indeg[b[i]]++; outdeg[a[i]]++; g[a[i]].push_back(b[i]); } int root = 0; rep(i, n) { if (indeg[i] == 0) { root = i; break; } } ans[root] = 0; dfs(root, -1); rep(i, n) { cout << ans[i] << endl; } }
// #include <bits/stdc++.h> #include <algorithm> #include <iostream> #include <array> #include <bitset> #include <complex> #include <cstring> #include <deque> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <numeric> #include <random> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ull; // typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<double, int> pdi; typedef pair<long double, int> pdbi; typedef pair<int, pii> pipii; typedef pair<ll, pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1 << 30; const ll INFLL = 1LL << 62; const long double EPS = 1e-12; const ll mod = 1000000000 + 7; // const ll mod = 998244353; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) \ (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false // debug #define DEBUG #define DUMPOUT cout #ifdef DEBUG #define dump(...) \ DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() { DUMPOUT << endl; }; template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } // ostream template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "["; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int n, m; int a[100000 + 5], b[100000 + 5]; vector<vector<int>> g(100000 + 5); vector<int> ans(100000 + 5, -1); vector<int> indeg(100000 + 5), outdeg(100000 + 5); vector<bool> visited(100000 + 5); void dfs(int now, int par) { visited[now] = true; if (par != -1 && ans[now] == -1) { ans[now] = par + 1; } for (auto nx : g[now]) { if (nx == par || visited[nx]) continue; if (indeg[nx] == 1) { for (auto nx2 : g[now]) { indeg[nx2]--; } dfs(nx, now); for (auto nx2 : g[now]) { indeg[nx2]++; } } } return; } void bfs(int r, int par) { queue<pii> q; q.push({r, par}); while (!q.empty()) { pii p = q.front(); q.pop(); int now = p.fi; int pa = p.se; visited[now] = true; if (now != r) ans[now] = pa + 1; for (auto nx : g[now]) { if (indeg[nx] == 1 && !visited[nx]) { visited[nx] = true; q.push({nx, now}); } } for (auto nx : g[now]) { indeg[nx]--; } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, n - 1 + m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; indeg[b[i]]++; outdeg[a[i]]++; g[a[i]].push_back(b[i]); } int root = 0; rep(i, n) { if (indeg[i] == 0) { root = i; break; } } ans[root] = 0; bfs(root, -1); rep(i, n) { cout << ans[i] << endl; } }
replace
217
218
217
218
TLE
p03142
C++
Time Limit Exceeded
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> using namespace ::std; // vector<int> find_reachable_child(int parent, vector<int> kodomo, // vector<vector<int> > &reachable) // { // for(int i(0); i < kodomo[parent].size(); ++i) // { // int child = kodomo[parent][i]; // reachable[parent][child] = 1; // find_reachable_child(child, kodomo, reachable); // } // return kodomo; // } // bool is_reachable(int child, int parent, vector<vector <int> > oya, int // depth) // { // if(child == parent) // { // if(depth > 2) // return true; // } // if(oya[child].size() == 0) // return false; // for(int i(0); i < oya[child].size(); ++i) // { // int dep = depth; // if(is_reachable(oya[child][i], parent, oya, ++dep)) // return true; // } // return false; // } int main() { int n, m; cin >> n >> m; vector<int> a; vector<int> b; vector<vector<int>> oya(n + 1, vector<int>(0)); vector<vector<int>> kodomo(n + 1, vector<int>(0)); vector<int> motomoto(n + 1); for (int i(0); i < n + m - 1; ++i) { int ai, bi; cin >> ai >> bi; a.push_back(ai); b.push_back(bi); kodomo[ai].push_back(bi); oya[bi].push_back(ai); } int root; for (int i(1); i < n + 1; ++i) { if (oya[i].size() == 0) { motomoto[i] == 0; root = i; break; } } queue<int> q; q.push(root); while (!q.empty()) { int node = q.front(); q.pop(); for (int i(0); i < kodomo[node].size(); ++i) { int child = kodomo[node][i]; motomoto[child] = node; q.push(child); } } for (int i(1); i < n + 1; ++i) { cout << motomoto[i] << endl; } // cout << cnt << endl; return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> using namespace ::std; // vector<int> find_reachable_child(int parent, vector<int> kodomo, // vector<vector<int> > &reachable) // { // for(int i(0); i < kodomo[parent].size(); ++i) // { // int child = kodomo[parent][i]; // reachable[parent][child] = 1; // find_reachable_child(child, kodomo, reachable); // } // return kodomo; // } // bool is_reachable(int child, int parent, vector<vector <int> > oya, int // depth) // { // if(child == parent) // { // if(depth > 2) // return true; // } // if(oya[child].size() == 0) // return false; // for(int i(0); i < oya[child].size(); ++i) // { // int dep = depth; // if(is_reachable(oya[child][i], parent, oya, ++dep)) // return true; // } // return false; // } int main() { int n, m; cin >> n >> m; vector<int> a; vector<int> b; vector<vector<int>> oya(n + 1, vector<int>(0)); vector<vector<int>> kodomo(n + 1, vector<int>(0)); vector<int> motomoto(n + 1); for (int i(0); i < n + m - 1; ++i) { int ai, bi; cin >> ai >> bi; a.push_back(ai); b.push_back(bi); kodomo[ai].push_back(bi); oya[bi].push_back(ai); } int root; for (int i(1); i < n + 1; ++i) { if (oya[i].size() == 0) { motomoto[i] == 0; root = i; break; } } queue<int> q; q.push(root); while (!q.empty()) { int node = q.front(); q.pop(); for (int i(0); i < kodomo[node].size(); ++i) { int child = kodomo[node][i]; oya[child].erase(oya[child].end() - 1); if (oya[child].size() == 0) { motomoto[child] = node; q.push(child); } } } for (int i(1); i < n + 1; ++i) { cout << motomoto[i] << endl; } // cout << cnt << endl; return 0; }
replace
81
83
81
86
TLE
p03142
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int INF = 2e9 + 10; #define fi first #define se second #define pb push_back #define mp make_pair int read() { int xx = 0, ww = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') ww = -ww; ch = getchar(); } while (ch >= '0' && ch <= '9') { xx = 10 * xx + ch - 48; ch = getchar(); } return xx * ww; } int n, m, de[10010]; int from, to, fa[10010]; vector<int> ed[10010]; queue<int> q; int main() { n = read(); m = read(); for (int i = 1; i <= n + m - 1; i++) { from = read(); to = read(); ed[from].pb(to); de[to]++; } // for(int i=1; i<=n; i++) printf("%d\n",de[i]); for (int i = 1; i <= n; i++) if (de[i] == 0) { q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int j = 0; j < ed[u].size(); j++) { int v = ed[u][j]; de[v]--; if (de[v] == 0) { fa[v] = u; q.push(v); } } } fa[i] = 0; break; } for (int i = 1; i <= n; i++) printf("%d\n", fa[i]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int INF = 2e9 + 10; #define fi first #define se second #define pb push_back #define mp make_pair int read() { int xx = 0, ww = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') ww = -ww; ch = getchar(); } while (ch >= '0' && ch <= '9') { xx = 10 * xx + ch - 48; ch = getchar(); } return xx * ww; } int n, m, de[100010]; int from, to, fa[100010]; vector<int> ed[100010]; queue<int> q; int main() { n = read(); m = read(); for (int i = 1; i <= n + m - 1; i++) { from = read(); to = read(); ed[from].pb(to); de[to]++; } // for(int i=1; i<=n; i++) printf("%d\n",de[i]); for (int i = 1; i <= n; i++) if (de[i] == 0) { q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (int j = 0; j < ed[u].size(); j++) { int v = ed[u][j]; de[v]--; if (de[v] == 0) { fa[v] = u; q.push(v); } } } fa[i] = 0; break; } for (int i = 1; i <= n; i++) printf("%d\n", fa[i]); return 0; }
replace
26
29
26
29
0
p03142
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; #define Rep(b, e, i) for (ll i = b; i <= e; i++) #define Repr(e, b, i) for (ll i = e; i >= b; i--) #define rep(n, i) Rep(0, n - 1, i) #define repr(n, i) Repr(n - 1, 0, i) #define all(v) (v).begin(), (v).end() #define pb(v) push_back(v) #define uniq(v) (v).erase(unique(all(v)), (v).end()) #define bitcnt(x) __builtin_popcount(x) #define fst first #define snd second #define Pqaz(T) priority_queue<T, vector<T>, greater<T>> #define Pqza(T) priority_queue<T> #define put(x) cout << x; #define putsp(x) cout << x << ' '; #define putbd cout << "---------------------------------------------" << endl; #define putln(x) cout << x << endl; #define debug(x) cerr << #x << "=" << x << endl; #define ENJYU std::ios::sync_with_stdio(false) std::cin.tie(0); typedef long long ll; typedef pair<ll, ll> llP; typedef pair<int, int> intP; typedef complex<double> comp; typedef vector<int> vec; typedef vector<ll> vecll; typedef vector<double> vecd; typedef vector<vec> mat; typedef vector<vecll> matll; typedef vector<vecd> matd; template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "{"; for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i]; o << "}"; return o; } const int MAX = 1000010; const double PI = 3.14159265358979323846; const double EPS = 1e-12; const int INF = 1 << 29; const ll INFL = 1e18; const ll MOD = 1e9 + 7; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; // const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1}; //********************************template //END****************************************// void solve() { int N, M; cin >> N >> M; vector<int> adj[N]; int root = -1; vec dim(N); rep(N + M - 1, i) { int a, b; cin >> a >> b; a--, b--; adj[a].pb(b); dim[b]++; } rep(N, i) { if (!dim[i]) root = i; } vec par(N), vst(N); queue<int> q; q.push(root); while (!q.empty()) { int v = q.front(); q.pop(); if (vst[v]) continue; for (int nv : adj[v]) { par[nv] = v + 1; q.push(nv); } } rep(N, i) { cout << par[i] << endl; } } int main() { solve(); // cout << "yui(*-v・)yui" << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define Rep(b, e, i) for (ll i = b; i <= e; i++) #define Repr(e, b, i) for (ll i = e; i >= b; i--) #define rep(n, i) Rep(0, n - 1, i) #define repr(n, i) Repr(n - 1, 0, i) #define all(v) (v).begin(), (v).end() #define pb(v) push_back(v) #define uniq(v) (v).erase(unique(all(v)), (v).end()) #define bitcnt(x) __builtin_popcount(x) #define fst first #define snd second #define Pqaz(T) priority_queue<T, vector<T>, greater<T>> #define Pqza(T) priority_queue<T> #define put(x) cout << x; #define putsp(x) cout << x << ' '; #define putbd cout << "---------------------------------------------" << endl; #define putln(x) cout << x << endl; #define debug(x) cerr << #x << "=" << x << endl; #define ENJYU std::ios::sync_with_stdio(false) std::cin.tie(0); typedef long long ll; typedef pair<ll, ll> llP; typedef pair<int, int> intP; typedef complex<double> comp; typedef vector<int> vec; typedef vector<ll> vecll; typedef vector<double> vecd; typedef vector<vec> mat; typedef vector<vecll> matll; typedef vector<vecd> matd; template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "{"; for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i]; o << "}"; return o; } const int MAX = 1000010; const double PI = 3.14159265358979323846; const double EPS = 1e-12; const int INF = 1 << 29; const ll INFL = 1e18; const ll MOD = 1e9 + 7; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; // const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1}; //********************************template //END****************************************// void solve() { int N, M; cin >> N >> M; vector<int> adj[N]; int root = -1; vec dim(N); rep(N + M - 1, i) { int a, b; cin >> a >> b; a--, b--; adj[a].pb(b); dim[b]++; } rep(N, i) { if (!dim[i]) root = i; } vec par(N), vst(N); queue<int> q; q.push(root); while (!q.empty()) { int v = q.front(); q.pop(); if (vst[v]) continue; for (int nv : adj[v]) { dim[nv]--; if (!dim[nv]) { par[nv] = v + 1; q.push(nv); } } } rep(N, i) { cout << par[i] << endl; } } int main() { solve(); // cout << "yui(*-v・)yui" << endl; return 0; }
replace
81
83
81
86
TLE
p03143
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (n); i++) class UnionFind { private: vector<int> p, s, w; vector<vector<int>> h; public: UnionFind(const vector<int> &v) : p(v.size(), -1), s(v.size(), 1), w(v.begin(), v.end()) {} void wrap() { h.push_back({}); } void unwrap() { int k = h.back().size(); rep(i, k) { s[p[h.back()[k - 1 - i]]] -= s[h.back()[k - 1 - i]]; w[p[h.back()[k - 1 - i]]] -= w[h.back()[k - 1 - i]]; p[h.back()[k - 1 - i]] = -1; } h.pop_back(); } int find(int a) { if (p[a] < 0) { return a; } return find(p[a]); } void unite(int a, int b) { a = find(a); b = find(b); if (a != b) { if (s[a] >= s[b]) { h.back().push_back(b); s[a] += s[b]; w[a] += w[b]; p[b] = a; } else { h.back().push_back(a); s[b] += s[a]; w[b] += w[a]; p[a] = b; } } } int get(int a) { return w[find(a)]; } }; vector<int> v; vector<pair<int, pair<int, int>>> ed; vector<int> dfs(int b, int e, UnionFind &uf) { if (b == e - 1) { uf.wrap(); uf.unite(ed[b].second.first, ed[b].second.second); if (uf.get(ed[b].second.first) >= ed[b].first) { uf.unwrap(); return {b}; } else { uf.unwrap(); return {}; } } else { int m = (b + e) / 2; uf.wrap(); for (int i = b; i < m; i++) { uf.unite(ed[i].second.first, ed[i].second.second); } auto r1 = dfs(m, e, uf); uf.unwrap(); uf.wrap(); for (auto i : r1) { uf.unite(ed[i].second.first, ed[i].second.second); } auto r2 = dfs(b, m, uf); uf.unwrap(); r2.insert(r2.end(), r1.begin(), r1.end()); return r2; } } signed main() { int n, m; cin >> n >> m; rep(i, n) { int w; cin >> w; v.push_back(w); } rep(i, m) { int a, b, y; cin >> a >> b >> y; ed.push_back(make_pair(y, make_pair(a - 1, b - 1))); } sort(ed.begin(), ed.end()); UnionFind uf(v); cout << m - dfs(0, m, uf).size() << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (n); i++) class UnionFind { private: vector<int> p, s, w; vector<vector<int>> h; public: UnionFind(const vector<int> &v) : p(v.size(), -1), s(v.size(), 1), w(v.begin(), v.end()) {} void wrap() { h.push_back({}); } void unwrap() { int k = h.back().size(); rep(i, k) { s[p[h.back()[k - 1 - i]]] -= s[h.back()[k - 1 - i]]; w[p[h.back()[k - 1 - i]]] -= w[h.back()[k - 1 - i]]; p[h.back()[k - 1 - i]] = -1; } h.pop_back(); } int find(int a) { if (p[a] < 0) { return a; } return find(p[a]); } void unite(int a, int b) { a = find(a); b = find(b); if (a != b) { if (s[a] >= s[b]) { h.back().push_back(b); s[a] += s[b]; w[a] += w[b]; p[b] = a; } else { h.back().push_back(a); s[b] += s[a]; w[b] += w[a]; p[a] = b; } } } int get(int a) { return w[find(a)]; } }; vector<int> v; vector<pair<int, pair<int, int>>> ed; vector<int> dfs(int b, int e, UnionFind &uf) { if (b == e - 1) { uf.wrap(); uf.unite(ed[b].second.first, ed[b].second.second); if (uf.get(ed[b].second.first) >= ed[b].first) { uf.unwrap(); return {b}; } else { uf.unwrap(); return {}; } } else { int m = (b + e) / 2; uf.wrap(); for (int i = b; i < m; i++) { uf.unite(ed[i].second.first, ed[i].second.second); } auto r1 = dfs(m, e, uf); uf.unwrap(); uf.wrap(); for (auto i : r1) { uf.unite(ed[i].second.first, ed[i].second.second); } auto r2 = dfs(b, m, uf); uf.unwrap(); r2.insert(r2.end(), r1.begin(), r1.end()); return r2; } } signed main() { int n, m; cin >> n >> m; rep(i, n) { int w; cin >> w; v.push_back(w); } rep(i, m) { int a, b, y; cin >> a >> b >> y; ed.push_back(make_pair(y, make_pair(a - 1, b - 1))); } if (m == 0) { cout << 0 << endl; return 0; } sort(ed.begin(), ed.end()); UnionFind uf(v); cout << m - dfs(0, m, uf).size() << endl; }
insert
95
95
95
99
0
p03143
C++
Runtime Error
#include <bits/stdc++.h> using i64 = long long; template <class T = int> class UF { T *par; public: UF(T n) : par(new T[n]) { for (T i = 0; i < n; i++) { par[i] = i; } } ~UF() { delete[] par; } T root(T x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(T x, T y) { return root(x) == root(y); } void unite(T x, T y) { x = root(x); y = root(y); if (x != y) { par[x] = y; } } }; int main() { int n, m; std::cin >> n >> m; std::vector<i64> w(n); for (int i = 0; i < n; i++) { std::cin >> w[i]; } std::vector<std::pair<i64, std::pair<int, int>>> es; for (int i = 0; i < m; i++) { int a, b; i64 y; std::cin >> a >> b >> y; a--; b--; es.push_back({y, {a, b}}); } std::sort(es.begin(), es.end()); UF<> uft(n); std::vector<bool> cand(m), done(m); std::vector<std::deque<std::pair<int, int>>> g(n); for (int i = 0; i < m; i++) { auto a = es[i].second.first, b = es[i].second.second; auto y = es[i].first; auto xx = w[uft.root(a)] + (uft.same(a, b) ? 0 : w[uft.root(b)]); uft.unite(a, b); w[uft.root(a)] = xx; if (y <= xx) { cand[i] = true; } g[a].emplace_back(b, i); g[b].emplace_back(a, i); } int ret = 0; for (int i = m - 1; i >= 0; i--) { if (done[i]) continue; if (!cand[i]) { ret++; continue; } std::deque<int> q{es[i].second.first, es[i].second.second}; while (!q.empty()) { auto v = q.front(); q.pop_front(); for (auto el : g[v]) { if (!done[el.second] && es[el.second].first <= es[i].first) { q.push_back(el.second); done[el.second] = true; } } } } std::cout << ret << std::endl; return 0; }
#include <bits/stdc++.h> using i64 = long long; template <class T = int> class UF { T *par; public: UF(T n) : par(new T[n]) { for (T i = 0; i < n; i++) { par[i] = i; } } ~UF() { delete[] par; } T root(T x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(T x, T y) { return root(x) == root(y); } void unite(T x, T y) { x = root(x); y = root(y); if (x != y) { par[x] = y; } } }; int main() { int n, m; std::cin >> n >> m; std::vector<i64> w(n); for (int i = 0; i < n; i++) { std::cin >> w[i]; } std::vector<std::pair<i64, std::pair<int, int>>> es; for (int i = 0; i < m; i++) { int a, b; i64 y; std::cin >> a >> b >> y; a--; b--; es.push_back({y, {a, b}}); } std::sort(es.begin(), es.end()); UF<> uft(n); std::vector<bool> cand(m), done(m); std::vector<std::deque<std::pair<int, int>>> g(n); for (int i = 0; i < m; i++) { auto a = es[i].second.first, b = es[i].second.second; auto y = es[i].first; auto xx = w[uft.root(a)] + (uft.same(a, b) ? 0 : w[uft.root(b)]); uft.unite(a, b); w[uft.root(a)] = xx; if (y <= xx) { cand[i] = true; } g[a].emplace_back(b, i); g[b].emplace_back(a, i); } int ret = 0; for (int i = m - 1; i >= 0; i--) { if (done[i]) continue; if (!cand[i]) { ret++; continue; } std::deque<int> q{es[i].second.first, es[i].second.second}; while (!q.empty()) { auto v = q.front(); q.pop_front(); for (auto el : g[v]) { if (!done[el.second] && es[el.second].first <= es[i].first) { q.push_back(el.first); done[el.second] = true; } } } } std::cout << ret << std::endl; return 0; }
replace
80
81
80
81
0
p03143
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define REPD(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REP(i, n) REP2(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define PB(e) push_back(e) #define FOREACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define MP(a, b) make_pair(a, b) #define BIT(n, m) (((n) >> (m)) & 1) typedef long long ll; template <typename S, typename T> ostream &operator<<(ostream &out, const pair<S, T> &p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "["; REP(i, v.size()) { if (i > 0) out << ", "; out << v[i]; } out << "]"; return out; } class UnionFind { public: int num_components; std::vector<int> parent; std::vector<ll> weight; std::vector<int> rank; public: UnionFind(int N, const vector<ll> weight) : num_components(N), parent(std::vector<int>(N)), weight(weight), rank(std::vector<int>(N, 0)) { for (int i = 0; i < N; i++) parent[i] = i; } int find(int x) { if (x == parent[x]) return x; else return parent[x] = find(parent[x]); } int size(int x) { return weight[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; num_components--; if (rank[x] < rank[y]) { weight[y] += weight[x]; parent[x] = y; } else { weight[x] += weight[y]; parent[y] = x; if (rank[x] == rank[y]) rank[y]++; } } int count() { return num_components; } }; int main(int argc, char *argv[]) { int N, M; cin >> N >> M; vector<ll> WN(N); REP(i, N) cin >> WN[i]; vector<pair<ll, pair<int, int>>> E(M); REP(i, M) { int a, b, y; cin >> a >> b >> y; a--; b--; E[i] = make_pair(y, make_pair(a, b)); } sort(ALL(E)); UnionFind uf(N, WN); vector<tuple<ll, ll, ll>> op; vector<int> valid(N - 1, true); vector<int> last_op(N, -1); vector<vector<int>> ch_ops(N - 1); REP(i, M) { int w = E[i].first; // cout << w << endl; int a = E[i].second.first; int b = E[i].second.second; if (uf.same(a, b)) { continue; } // cout << "merge: " << a << " " << b << endl; a = uf.find(a); b = uf.find(b); int c = op.size(); if (last_op[a] != -1) { ch_ops[c].push_back(last_op[a]); } if (last_op[b] != -1) { ch_ops[c].push_back(last_op[b]); } op.push_back(make_tuple(uf.weight[a], uf.weight[b], i)); uf.unite(a, b); last_op[uf.find(a)] = c; } assert(int(op.size()) == N - 1); queue<int> que; que.push(N - 2); while (!que.empty()) { int i = que.front(); que.pop(); ll s0 = get<0>(op[i]); ll s1 = get<1>(op[i]); ll w = E[get<2>(op[i])].first; // cout << i << " " << w << " " << s0 << " "<< s1 << endl; if (w > s0 + s1) { valid[i] = false; for (int j : ch_ops[i]) { que.push(j); } } } UnionFind new_uf = UnionFind(N, WN); REP(i, N - 1) { if (valid[i]) { int eid = get<2>(op[i]); int a = E[eid].second.first; int b = E[eid].second.second; new_uf.unite(a, b); } } ll res = 0; REP(i, M) { int w = E[i].first; int a = new_uf.find(E[i].second.first); int b = new_uf.find(E[i].second.second); if (!new_uf.same(a, b) || w > new_uf.weight[a]) { res++; } } cout << res << endl; return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define REPD(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REP(i, n) REP2(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define PB(e) push_back(e) #define FOREACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define MP(a, b) make_pair(a, b) #define BIT(n, m) (((n) >> (m)) & 1) typedef long long ll; template <typename S, typename T> ostream &operator<<(ostream &out, const pair<S, T> &p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << "["; REP(i, v.size()) { if (i > 0) out << ", "; out << v[i]; } out << "]"; return out; } class UnionFind { public: int num_components; std::vector<int> parent; std::vector<ll> weight; std::vector<int> rank; public: UnionFind(int N, const vector<ll> weight) : num_components(N), parent(std::vector<int>(N)), weight(weight), rank(std::vector<int>(N, 0)) { for (int i = 0; i < N; i++) parent[i] = i; } int find(int x) { if (x == parent[x]) return x; else return parent[x] = find(parent[x]); } int size(int x) { return weight[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; num_components--; if (rank[x] < rank[y]) { weight[y] += weight[x]; parent[x] = y; } else { weight[x] += weight[y]; parent[y] = x; if (rank[x] == rank[y]) rank[y]++; } } int count() { return num_components; } }; int main(int argc, char *argv[]) { int N, M; cin >> N >> M; vector<ll> WN(N); REP(i, N) cin >> WN[i]; vector<pair<ll, pair<int, int>>> E(M); REP(i, M) { int a, b, y; cin >> a >> b >> y; a--; b--; E[i] = make_pair(y, make_pair(a, b)); } sort(ALL(E)); UnionFind uf(N, WN); vector<tuple<ll, ll, ll>> op; vector<int> valid(N - 1, true); vector<int> last_op(N, -1); vector<vector<int>> ch_ops(N - 1); if (N == 1) { cout << 0 << endl; return 0; } REP(i, M) { int w = E[i].first; // cout << w << endl; int a = E[i].second.first; int b = E[i].second.second; if (uf.same(a, b)) { continue; } // cout << "merge: " << a << " " << b << endl; a = uf.find(a); b = uf.find(b); int c = op.size(); if (last_op[a] != -1) { ch_ops[c].push_back(last_op[a]); } if (last_op[b] != -1) { ch_ops[c].push_back(last_op[b]); } op.push_back(make_tuple(uf.weight[a], uf.weight[b], i)); uf.unite(a, b); last_op[uf.find(a)] = c; } assert(int(op.size()) == N - 1); queue<int> que; que.push(N - 2); while (!que.empty()) { int i = que.front(); que.pop(); ll s0 = get<0>(op[i]); ll s1 = get<1>(op[i]); ll w = E[get<2>(op[i])].first; // cout << i << " " << w << " " << s0 << " "<< s1 << endl; if (w > s0 + s1) { valid[i] = false; for (int j : ch_ops[i]) { que.push(j); } } } UnionFind new_uf = UnionFind(N, WN); REP(i, N - 1) { if (valid[i]) { int eid = get<2>(op[i]); int a = E[eid].second.first; int b = E[eid].second.second; new_uf.unite(a, b); } } ll res = 0; REP(i, M) { int w = E[i].first; int a = new_uf.find(E[i].second.first); int b = new_uf.find(E[i].second.second); if (!new_uf.same(a, b) || w > new_uf.weight[a]) { res++; } } cout << res << endl; return 0; }
insert
123
123
123
128
0
p03143
C++
Runtime Error
#include "bits/stdc++.h" #define YES "YES" #define NO "NO" #define YESNO OUT(three(solve(), YES, NO)) #define ECHO OUT(solve()) #define three(A, B, C) ((A) ? (B) : (C)) #define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++) #define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++) #define RFOR(i, a, b) for (LL i = (b); i >= (LL)(a); i--) #define REP(i, b) FOR(i, zero, b) #define rep REP #define EREP(i, b) EFOR(i, zero, b) #define RREP(i, b) RFOR(i, b, zero) #define ALL(c) c.begin(), c.end() #define UNIQUE(c) \ sort(ALL(c)); \ c.erase(unique(ALL(c)), c.end()) #define MAX(c) (*max_element(ALL(c))) #define MIN(c) (*min_element(ALL(c))) #define MP make_pair #define FI first #define SE second #define SI(x) (LL(x.size())) #define PB push_back #define DEBUG(a) OUT(a) #define DEBUG2(a, b) OUT2(a, b) #define cat cout << __LINE__ << endl #define OUT(a) cout << (a) << endl #define OUT2(a, b) cout << (a) << " " << (b) << endl #define zero 0LL #define all ALL #define pb emplace_back #define eb pb #define int long long using namespace std; template <typename T> inline void maximize(T &a, T b) { a = max(a, b); } template <typename T> inline void minimize(T &a, T b) { a = min(a, b); } template <typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } typedef int LL; typedef double ld; typedef int ut; typedef vector<ut> VI; typedef vector<VI> VII; typedef pair<ut, ut> pr; typedef pair<ut, pr> ppr; typedef vector<pr> Vpr; typedef vector<ppr> Vppr; typedef tuple<int, int, int, int> tapu; typedef vector<tapu> Vtapu; typedef priority_queue<ppr, Vppr, greater<ppr>> PQ; inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; } OUT(""); } const int SIZE1 = 3e5 + 1000; const int SIZE2 = 2010; const int SIZE3 = 430; const int SIZE = SIZE1; const int MAPSIZE = 40; const LL p = 7 + 1e9; const LL INF = 1LL << 60; const long double EPS = 1e-7; typedef pair<ld, ut> pld; VI evals[SIZE]; LL maxim[SIZE]; LL anses = 0; LL X[SIZE]; class UnionFind { vector<int> unions, talls, sents, dists, sizes; int size; public: UnionFind() {} UnionFind(int size) : size(size) { init(); } void init() { unions = vector<int>(size); talls = vector<int>(size); sents = vector<int>(size); dists = vector<int>(size); sizes = vector<int>(size); FOR(i, 0, size) { unions[i] = i; sents[i] = 0; sizes[i] = 0; // ns[i].PB(pr(0,1)); talls[i] = 1; } } int find(int x, int t = 0) { if (unions[x] == x || sents[x] > t) return x; return find(unions[x], t); } int minisize(int x) { if (x == unions[x]) return sizes[x]; return minisize(find(x)); } bool isFriend(int a, int b, int t = 0) { return find(a, t) == find(b, t); } void unite(int a, int b, LL y, int t = 0) { a = find(a, t); b = find(b, t); if (talls[a] < talls[b]) unite(b, a, t); if (isFriend(a, b, t)) { sizes[a] += 1; MX(maxim[a], y); assert(maxim[a] <= y); if (maxim[a] <= X[a]) { assert(sizes[a] == 1); anses += sizes[a]; sizes[a] = 0; } return; } else { MX(maxim[a], maxim[b]); // cout << maxim[a] << " " << y << endl; assert(maxim[a] <= y); MX(maxim[a], y); sizes[a] += sizes[b] + 1; sizes[b] = 0; X[a] += X[b]; X[b] = 0; if (maxim[a] <= X[a]) { anses += sizes[a]; sizes[a] = 0; } sents[b] = t; talls[a] = max(talls[a], talls[b] + 1); unions[b] = a; dists[b] = 1; // ns[a].PB(pr(t,ns[b].back().second+ns[a].back().second)); } } }; // #define endl "\n" //インタラクティブでは消す // ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U; ut N, M, K, L, Q, D, H, W, R; VI edges[SIZE]; LL vals[SIZE], answer = zero; LL maps[SIZE2][SIZE2]; LL solve() { cin >> N >> M; EFOR(i, 1, N) cin >> X[i]; Vppr v; REP(i, M) { LL a, b, y; cin >> a >> b >> y; v.pb(y, pr(a, b)); } sort(ALL(v)); UnionFind uf = UnionFind(N + 1); REP(i, v.size()) { LL a = v[i].second.first, b = v[i].second.second, y = v[i].first; uf.unite(a, b, y); // cout << a << " " << b << " " << anses << endl; } assert(0 <= anses and anses <= M); cout << M - anses << endl; return 0; } //!!!!!!!!!!!!!!!!!!!実装を詰める!!!!!!!!!!!!!!!!!!!!!!!!! signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); string s; solve(); // cin >> N; return 0; }
#include "bits/stdc++.h" #define YES "YES" #define NO "NO" #define YESNO OUT(three(solve(), YES, NO)) #define ECHO OUT(solve()) #define three(A, B, C) ((A) ? (B) : (C)) #define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++) #define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++) #define RFOR(i, a, b) for (LL i = (b); i >= (LL)(a); i--) #define REP(i, b) FOR(i, zero, b) #define rep REP #define EREP(i, b) EFOR(i, zero, b) #define RREP(i, b) RFOR(i, b, zero) #define ALL(c) c.begin(), c.end() #define UNIQUE(c) \ sort(ALL(c)); \ c.erase(unique(ALL(c)), c.end()) #define MAX(c) (*max_element(ALL(c))) #define MIN(c) (*min_element(ALL(c))) #define MP make_pair #define FI first #define SE second #define SI(x) (LL(x.size())) #define PB push_back #define DEBUG(a) OUT(a) #define DEBUG2(a, b) OUT2(a, b) #define cat cout << __LINE__ << endl #define OUT(a) cout << (a) << endl #define OUT2(a, b) cout << (a) << " " << (b) << endl #define zero 0LL #define all ALL #define pb emplace_back #define eb pb #define int long long using namespace std; template <typename T> inline void maximize(T &a, T b) { a = max(a, b); } template <typename T> inline void minimize(T &a, T b) { a = min(a, b); } template <typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } typedef int LL; typedef double ld; typedef int ut; typedef vector<ut> VI; typedef vector<VI> VII; typedef pair<ut, ut> pr; typedef pair<ut, pr> ppr; typedef vector<pr> Vpr; typedef vector<ppr> Vppr; typedef tuple<int, int, int, int> tapu; typedef vector<tapu> Vtapu; typedef priority_queue<ppr, Vppr, greater<ppr>> PQ; inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; } OUT(""); } const int SIZE1 = 3e5 + 1000; const int SIZE2 = 2010; const int SIZE3 = 430; const int SIZE = SIZE1; const int MAPSIZE = 40; const LL p = 7 + 1e9; const LL INF = 1LL << 60; const long double EPS = 1e-7; typedef pair<ld, ut> pld; VI evals[SIZE]; LL maxim[SIZE]; LL anses = 0; LL X[SIZE]; class UnionFind { vector<int> unions, talls, sents, dists, sizes; int size; public: UnionFind() {} UnionFind(int size) : size(size) { init(); } void init() { unions = vector<int>(size); talls = vector<int>(size); sents = vector<int>(size); dists = vector<int>(size); sizes = vector<int>(size); FOR(i, 0, size) { unions[i] = i; sents[i] = 0; sizes[i] = 0; // ns[i].PB(pr(0,1)); talls[i] = 1; } } int find(int x, int t = 0) { if (unions[x] == x || sents[x] > t) return x; return find(unions[x], t); } int minisize(int x) { if (x == unions[x]) return sizes[x]; return minisize(find(x)); } bool isFriend(int a, int b, int t = 0) { return find(a, t) == find(b, t); } void unite(int a, int b, LL y, int t = 0) { a = find(a, t); b = find(b, t); if (talls[a] < talls[b]) unite(b, a, y, t); if (isFriend(a, b, t)) { sizes[a] += 1; MX(maxim[a], y); assert(maxim[a] <= y); if (maxim[a] <= X[a]) { assert(sizes[a] == 1); anses += sizes[a]; sizes[a] = 0; } return; } else { MX(maxim[a], maxim[b]); // cout << maxim[a] << " " << y << endl; assert(maxim[a] <= y); MX(maxim[a], y); sizes[a] += sizes[b] + 1; sizes[b] = 0; X[a] += X[b]; X[b] = 0; if (maxim[a] <= X[a]) { anses += sizes[a]; sizes[a] = 0; } sents[b] = t; talls[a] = max(talls[a], talls[b] + 1); unions[b] = a; dists[b] = 1; // ns[a].PB(pr(t,ns[b].back().second+ns[a].back().second)); } } }; // #define endl "\n" //インタラクティブでは消す // ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U; ut N, M, K, L, Q, D, H, W, R; VI edges[SIZE]; LL vals[SIZE], answer = zero; LL maps[SIZE2][SIZE2]; LL solve() { cin >> N >> M; EFOR(i, 1, N) cin >> X[i]; Vppr v; REP(i, M) { LL a, b, y; cin >> a >> b >> y; v.pb(y, pr(a, b)); } sort(ALL(v)); UnionFind uf = UnionFind(N + 1); REP(i, v.size()) { LL a = v[i].second.first, b = v[i].second.second, y = v[i].first; uf.unite(a, b, y); // cout << a << " " << b << " " << anses << endl; } assert(0 <= anses and anses <= M); cout << M - anses << endl; return 0; } //!!!!!!!!!!!!!!!!!!!実装を詰める!!!!!!!!!!!!!!!!!!!!!!!!! signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); string s; solve(); // cin >> N; return 0; }
replace
113
114
113
114
0
p03143
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long // aaaaaaaaaaaaaaaaaa #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define allr(x) (x).rbegin(), (x).rend() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define SIZE(buff) (sizeof(buff) / sizeof(buff[0])) typedef vector<int> VI; typedef vector<vector<int>> VVI; typedef long long ll; typedef pair<int, int> PII; typedef vector<pair<int, int>> VPII; const ll mod = 1000000007; ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } void chmin(int64_t &a, int64_t b) { a = min(a, b); } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "{"; for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i]; o << "}"; return o; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // head int q, m, n, k, res, tmp, tmp2, tmp3, h, w; int a[100001] = {}; // int b[100010] = {}; deque<PII> que; string s; map<int, VPII> kukan; vector<pair<int, PII>> g; int dp[200000]; int flag; int ok[100010] = {}; VPII g2[200000]; class UnionFind { private: int n; vector<int> uni; public: UnionFind(int _n) { n = _n; uni.clear(); uni.resize(n, -1); rep(i, 0, n) { uni[i] = -a[i]; } } int root(int x) { if (uni[x] < 0) return x; return uni[x] = root(uni[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (uni[x] > uni[y]) swap(x, y); uni[x] += uni[y]; uni[y] = x; } int getSize(int x) { return -uni[root(x)]; } void print() { for (auto x : uni) cout << x << " "; cout << endl; } }; void dfs(int x, int c) { rep(j, 0, g2[x].size()) { PII i = g2[x][j]; if (i.se <= c) { g2[x][j].se = 1LL << 60; res += 1; dfs(i.fi, c); } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, 0, n) { cin >> a[i]; } rep(i, 0, m) { cin >> tmp >> tmp2 >> tmp3; g.pb({tmp3, {tmp2 - 1, tmp - 1}}); g2[tmp2 - 1].push_back({tmp - 1, tmp3}); g2[tmp - 1].push_back({tmp2 - 1, tmp3}); } UnionFind UF(n); sort(all(g)); VI v; rep(i, 0, n) { pair<int, PII> hen = g[i]; UF.unite(hen.se.fi, hen.se.se); if (UF.getSize(hen.se.fi) >= hen.fi) { v.pb(i); } } res = 0; reverse(all(v)); for (int k : v) { pair<int, PII> hen = g[k]; dfs(hen.se.se, hen.fi); } cout << m - res / 2 << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long // aaaaaaaaaaaaaaaaaa #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define allr(x) (x).rbegin(), (x).rend() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define SIZE(buff) (sizeof(buff) / sizeof(buff[0])) typedef vector<int> VI; typedef vector<vector<int>> VVI; typedef long long ll; typedef pair<int, int> PII; typedef vector<pair<int, int>> VPII; const ll mod = 1000000007; ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } void chmin(int64_t &a, int64_t b) { a = min(a, b); } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "{"; for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i]; o << "}"; return o; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // head int q, m, n, k, res, tmp, tmp2, tmp3, h, w; int a[100001] = {}; // int b[100010] = {}; deque<PII> que; string s; map<int, VPII> kukan; vector<pair<int, PII>> g; int dp[200000]; int flag; int ok[100010] = {}; VPII g2[200000]; class UnionFind { private: int n; vector<int> uni; public: UnionFind(int _n) { n = _n; uni.clear(); uni.resize(n, -1); rep(i, 0, n) { uni[i] = -a[i]; } } int root(int x) { if (uni[x] < 0) return x; return uni[x] = root(uni[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (uni[x] > uni[y]) swap(x, y); uni[x] += uni[y]; uni[y] = x; } int getSize(int x) { return -uni[root(x)]; } void print() { for (auto x : uni) cout << x << " "; cout << endl; } }; void dfs(int x, int c) { rep(j, 0, g2[x].size()) { PII i = g2[x][j]; if (i.se <= c) { g2[x][j].se = 1LL << 60; res += 1; dfs(i.fi, c); } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; rep(i, 0, n) { cin >> a[i]; } rep(i, 0, m) { cin >> tmp >> tmp2 >> tmp3; g.pb({tmp3, {tmp2 - 1, tmp - 1}}); g2[tmp2 - 1].push_back({tmp - 1, tmp3}); g2[tmp - 1].push_back({tmp2 - 1, tmp3}); } UnionFind UF(n); sort(all(g)); VI v; rep(i, 0, m) { pair<int, PII> hen = g[i]; UF.unite(hen.se.fi, hen.se.se); if (UF.getSize(hen.se.fi) >= hen.fi) { v.pb(i); } } res = 0; reverse(all(v)); for (int k : v) { pair<int, PII> hen = g[k]; dfs(hen.se.se, hen.fi); } cout << m - res / 2 << endl; }
replace
118
119
118
119
0
p03143
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <thread> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned long long int ull; typedef long long int ll; typedef pair<ll, ll> pll; typedef pair<int, int> pi; typedef pair<double, double> pd; typedef pair<double, ll> pdl; #define F first #define S second const ll E = 1e18 + 7; const ll MOD = 1000000007; vector<pll> uft; ll parent(ll a) { return uft[a].F == a ? a : uft[a].F = parent(uft[a].F); } void merge(ll p, ll c) { if (parent(p) == parent(c)) { return; } uft[parent(p)].S += uft[parent(c)].S; uft[parent(c)].F = parent(p); } int main() { ll n, m; cin >> n >> m; vector<ll> X(n); for (auto &I : X) { cin >> I; } uft.resize(n + m); for (int i = 0; i < n; i++) { uft[i] = {i, X[i]}; } for (int i = 0; i < m; i++) { uft[i + n] = {i + n, 0}; } vector<pair<ll, pll>> edge(m); for (auto &I : edge) { cin >> I.S.F >> I.S.S >> I.F; I.S.F--; I.S.S--; } sort(edge.begin(), edge.end()); vector<pll> children(m, {-1, -1}); vector<bool> used(m, false); for (int i = 0; i < m; i++) { if (parent(edge[i].S.F) == parent(edge[i].S.S)) { continue; } used[i] = true; if (parent(edge[i].S.F) >= n && uft[parent(edge[i].S.F)].S < edge[parent(edge[i].S.F) - n].F) { children[i].F = parent(edge[i].S.F) - n; } if (parent(edge[i].S.S) >= n && uft[parent(edge[i].S.S)].S < edge[parent(edge[i].S.S) - n].F) { children[i].S = parent(edge[i].S.S) - n; } merge(i + n, edge[i].S.F); merge(i + n, edge[i].S.S); } if (uft[parent(0)].S < edge[parent(0) - n].F) { queue<ll> Q; Q.push(parent(0) - n); while (!Q.empty()) { ll w = Q.front(); Q.pop(); used[w] = false; if (children[w].F != -1) { Q.push(children[w].F); } if (children[w].S != -1) { Q.push(children[w].S); } } } ll ans = 0; for (int i = 0; i < n; i++) { uft[i] = {i, X[i]}; } for (int i = 0; i < m; i++) { if (used[i]) { ans++; merge(edge[i].S.F, edge[i].S.S); } } for (int i = 0; i < m; i++) { if (!used[i]) { if (parent(edge[i].S.F) == parent(edge[i].S.S) && uft[parent(edge[i].S.S)].S >= edge[i].F) { ans++; } } } cout << m - ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <thread> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned long long int ull; typedef long long int ll; typedef pair<ll, ll> pll; typedef pair<int, int> pi; typedef pair<double, double> pd; typedef pair<double, ll> pdl; #define F first #define S second const ll E = 1e18 + 7; const ll MOD = 1000000007; vector<pll> uft; ll parent(ll a) { return uft[a].F == a ? a : uft[a].F = parent(uft[a].F); } void merge(ll p, ll c) { if (parent(p) == parent(c)) { return; } uft[parent(p)].S += uft[parent(c)].S; uft[parent(c)].F = parent(p); } int main() { ll n, m; cin >> n >> m; vector<ll> X(n); for (auto &I : X) { cin >> I; } if (n == 1) { cout << 0 << endl; return 0; } uft.resize(n + m); for (int i = 0; i < n; i++) { uft[i] = {i, X[i]}; } for (int i = 0; i < m; i++) { uft[i + n] = {i + n, 0}; } vector<pair<ll, pll>> edge(m); for (auto &I : edge) { cin >> I.S.F >> I.S.S >> I.F; I.S.F--; I.S.S--; } sort(edge.begin(), edge.end()); vector<pll> children(m, {-1, -1}); vector<bool> used(m, false); for (int i = 0; i < m; i++) { if (parent(edge[i].S.F) == parent(edge[i].S.S)) { continue; } used[i] = true; if (parent(edge[i].S.F) >= n && uft[parent(edge[i].S.F)].S < edge[parent(edge[i].S.F) - n].F) { children[i].F = parent(edge[i].S.F) - n; } if (parent(edge[i].S.S) >= n && uft[parent(edge[i].S.S)].S < edge[parent(edge[i].S.S) - n].F) { children[i].S = parent(edge[i].S.S) - n; } merge(i + n, edge[i].S.F); merge(i + n, edge[i].S.S); } if (uft[parent(0)].S < edge[parent(0) - n].F) { queue<ll> Q; Q.push(parent(0) - n); while (!Q.empty()) { ll w = Q.front(); Q.pop(); used[w] = false; if (children[w].F != -1) { Q.push(children[w].F); } if (children[w].S != -1) { Q.push(children[w].S); } } } ll ans = 0; for (int i = 0; i < n; i++) { uft[i] = {i, X[i]}; } for (int i = 0; i < m; i++) { if (used[i]) { ans++; merge(edge[i].S.F, edge[i].S.S); } } for (int i = 0; i < m; i++) { if (!used[i]) { if (parent(edge[i].S.F) == parent(edge[i].S.S) && uft[parent(edge[i].S.S)].S >= edge[i].F) { ans++; } } } cout << m - ans << endl; return 0; }
insert
55
55
55
59
0
p03143
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <map> #include <random> #include <set> #include <time.h> #include <unordered_map> #include <vector> #define hash akasd #define prev aaaasds #define rank asdfdsf using namespace std; typedef long long ll; typedef long double dbl; template <class T> void print(vector<T> s) { for (T i : s) cout << i << " "; cout << endl; } const int maxn = 1e5 + 7; int prev[maxn], rank[maxn]; int getPrev(int v) { if (v == prev[v]) return v; prev[v] = getPrev(prev[v]); return prev[v]; } ll summa[maxn]; void join(int a, int b) { a = getPrev(a); b = getPrev(b); if (a == b) return; if (rank[a] < rank[b]) swap(a, b); prev[b] = a; summa[a] += summa[b]; if (rank[a] == rank[b]) rank[a]++; } ll kek[maxn]; vector<pair<int, int>> g[maxn]; bool ok[maxn]; void dfs(int v, ll x) { ok[v] = 1; for (pair<int, int> u : g[v]) { if (!ok[u.second] && u.first <= x) dfs(u.second, x); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); freopen("test.in", "r", stdin); int n, m; cin >> n >> m; vector<int> s(n); for (int i = 0; i < n; i++) cin >> s[i]; vector<pair<int, pair<int, int>>> lst(m); for (int i = 0; i < m; i++) { int a, b, x; cin >> a >> b >> x; lst[i] = {x, {a - 1, b - 1}}; g[a - 1].push_back({x, b - 1}); g[b - 1].push_back({x, a - 1}); } sort(lst.begin(), lst.end()); for (int i = 0; i < n; i++) { prev[i] = i; summa[i] = s[i]; } for (int i = 0; i < m; i++) { join(lst[i].second.first, lst[i].second.second); kek[i] = summa[getPrev(lst[i].second.first)]; } // for (int i = 0; i < m; i++) // cout << kek[i] << " "; // cout << endl; int answer = 0; for (int i = m - 1; i >= 0; i--) { int a = lst[i].second.first, b = lst[i].second.second; if (ok[a] && ok[b]) { answer++; continue; } if (lst[i].first <= kek[i]) { answer++; dfs(a, kek[i]); } } cout << m - answer << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <map> #include <random> #include <set> #include <time.h> #include <unordered_map> #include <vector> #define hash akasd #define prev aaaasds #define rank asdfdsf using namespace std; typedef long long ll; typedef long double dbl; template <class T> void print(vector<T> s) { for (T i : s) cout << i << " "; cout << endl; } const int maxn = 1e5 + 7; int prev[maxn], rank[maxn]; int getPrev(int v) { if (v == prev[v]) return v; prev[v] = getPrev(prev[v]); return prev[v]; } ll summa[maxn]; void join(int a, int b) { a = getPrev(a); b = getPrev(b); if (a == b) return; if (rank[a] < rank[b]) swap(a, b); prev[b] = a; summa[a] += summa[b]; if (rank[a] == rank[b]) rank[a]++; } ll kek[maxn]; vector<pair<int, int>> g[maxn]; bool ok[maxn]; void dfs(int v, ll x) { ok[v] = 1; for (pair<int, int> u : g[v]) { if (!ok[u.second] && u.first <= x) dfs(u.second, x); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("test.in", "r", stdin); int n, m; cin >> n >> m; vector<int> s(n); for (int i = 0; i < n; i++) cin >> s[i]; vector<pair<int, pair<int, int>>> lst(m); for (int i = 0; i < m; i++) { int a, b, x; cin >> a >> b >> x; lst[i] = {x, {a - 1, b - 1}}; g[a - 1].push_back({x, b - 1}); g[b - 1].push_back({x, a - 1}); } sort(lst.begin(), lst.end()); for (int i = 0; i < n; i++) { prev[i] = i; summa[i] = s[i]; } for (int i = 0; i < m; i++) { join(lst[i].second.first, lst[i].second.second); kek[i] = summa[getPrev(lst[i].second.first)]; } // for (int i = 0; i < m; i++) // cout << kek[i] << " "; // cout << endl; int answer = 0; for (int i = m - 1; i >= 0; i--) { int a = lst[i].second.first, b = lst[i].second.second; if (ok[a] && ok[b]) { answer++; continue; } if (lst[i].first <= kek[i]) { answer++; dfs(a, kek[i]); } } cout << m - answer << endl; return 0; }
replace
68
69
68
69
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03143
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, n) for (int i = 1; i <= (n); i++) #define all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end()) #define bit(n) (1LL << (n)) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double pi = 3.1415926535897932384626433832795028; constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int gcd(int a, int b) { while (b) { swap(a %= b, b); } return a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) { if (a < b) { a = b; return true; } return false; } template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; struct union_find_tree { int n; std::vector<int> par, size; vint last_edge, weight_sum; union_find_tree(int n) : n(n), par(n), size(n), last_edge(n), weight_sum(n) { for (int i = 0; i < n; i++) { par[i] = i; size[i] = 1; last_edge[i] = -1; } } int get_root(int i) { if (par[i] == i) { return i; } else { return par[i] = get_root(par[i]); } } void unite(int i, int j, int edge) { if ((i = get_root(i)) == (j = get_root(j))) { return; } if (size[i] < size[j]) { std::swap(i, j); } par[j] = i; last_edge[i] = edge; weight_sum[i] += weight_sum[j]; size[i] += size[j]; } bool are_same(int i, int j) { return get_root(i) == get_root(j); } int get_size(int i) { return size[get_root(i)]; } }; int N, M; int X[110000]; int A[110000], B[110000], Y[110000]; pair<int, pint> edges[110000]; vint edge_tree[110000]; bool prev_ok[110000], final_ok[110000]; void dfs(int idx) { if (idx == -1) { return; } if (prev_ok[idx]) { return; } final_ok[idx] = false; rep(i, edge_tree[idx].size()) { dfs(edge_tree[idx][i]); } } signed main() { cin >> N >> M; reps(i, N) { cin >> X[i]; } rep(i, M) { cin >> A[i] >> B[i] >> Y[i]; edges[i] = {Y[i], {A[i], B[i]}}; } union_find_tree uft0(N + 1); reps(i, N) { uft0.weight_sum[i] = X[i]; } sort(edges, edges + M); int le; rep(i, M) { int a = edges[i].second.first, b = edges[i].second.second, y = edges[i].first; a = uft0.get_root(a), b = uft0.get_root(b); if (uft0.are_same(a, b)) { continue; } le = i; final_ok[i] = true; edge_tree[i].push_back(uft0.last_edge[a]); edge_tree[i].push_back(uft0.last_edge[b]); prev_ok[i] = y <= uft0.weight_sum[a] + uft0.weight_sum[b]; uft0.unite(a, b, i); } dfs(le); union_find_tree uft1(N + 1); reps(i, N) { uft1.weight_sum[i] = X[i]; } rep(i, M) { int a = edges[i].second.first, b = edges[i].second.second; if (final_ok[i]) { uft1.unite(a, b, i); } } int ans = M; rep(i, M) { int a = edges[i].second.first, y = edges[i].first; a = uft1.get_root(a); ans -= y <= uft1.weight_sum[a]; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, n) for (int i = 1; i <= (n); i++) #define all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end()) #define bit(n) (1LL << (n)) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double pi = 3.1415926535897932384626433832795028; constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int gcd(int a, int b) { while (b) { swap(a %= b, b); } return a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) { if (a < b) { a = b; return true; } return false; } template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; struct union_find_tree { int n; std::vector<int> par, size; vint last_edge, weight_sum; union_find_tree(int n) : n(n), par(n), size(n), last_edge(n), weight_sum(n) { for (int i = 0; i < n; i++) { par[i] = i; size[i] = 1; last_edge[i] = -1; } } int get_root(int i) { if (par[i] == i) { return i; } else { return par[i] = get_root(par[i]); } } void unite(int i, int j, int edge) { if ((i = get_root(i)) == (j = get_root(j))) { return; } if (size[i] < size[j]) { std::swap(i, j); } par[j] = i; last_edge[i] = edge; weight_sum[i] += weight_sum[j]; size[i] += size[j]; } bool are_same(int i, int j) { return get_root(i) == get_root(j); } int get_size(int i) { return size[get_root(i)]; } }; int N, M; int X[110000]; int A[110000], B[110000], Y[110000]; pair<int, pint> edges[110000]; vint edge_tree[110000]; bool prev_ok[110000], final_ok[110000]; void dfs(int idx) { if (idx == -1) { return; } if (prev_ok[idx]) { return; } final_ok[idx] = false; rep(i, edge_tree[idx].size()) { dfs(edge_tree[idx][i]); } } signed main() { cin >> N >> M; if (N == 1) { cout << 0 << endl; return 0; } reps(i, N) { cin >> X[i]; } rep(i, M) { cin >> A[i] >> B[i] >> Y[i]; edges[i] = {Y[i], {A[i], B[i]}}; } union_find_tree uft0(N + 1); reps(i, N) { uft0.weight_sum[i] = X[i]; } sort(edges, edges + M); int le; rep(i, M) { int a = edges[i].second.first, b = edges[i].second.second, y = edges[i].first; a = uft0.get_root(a), b = uft0.get_root(b); if (uft0.are_same(a, b)) { continue; } le = i; final_ok[i] = true; edge_tree[i].push_back(uft0.last_edge[a]); edge_tree[i].push_back(uft0.last_edge[b]); prev_ok[i] = y <= uft0.weight_sum[a] + uft0.weight_sum[b]; uft0.unite(a, b, i); } dfs(le); union_find_tree uft1(N + 1); reps(i, N) { uft1.weight_sum[i] = X[i]; } rep(i, M) { int a = edges[i].second.first, b = edges[i].second.second; if (final_ok[i]) { uft1.unite(a, b, i); } } int ans = M; rep(i, M) { int a = edges[i].second.first, y = edges[i].first; a = uft1.get_root(a); ans -= y <= uft1.weight_sum[a]; } cout << ans << endl; }
insert
116
116
116
120
0
p03143
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <set> #include <vector> #define MAXN 100005 #define INF 0x3f3f3f3f #define rint register int #define LL long long #define LD long double using namespace std; int n, m, cnt, ans, head[MAXN], f[MAXN], b[MAXN]; LL num[MAXN]; struct Edge { int next, to; LL dis; } edge[MAXN * 2]; struct E { int x, y; LL z; } e[MAXN]; void addedge(int from, int to, LL dis) { edge[++cnt].next = head[from]; edge[cnt].to = to; edge[++cnt].dis = dis; head[from] = cnt; } bool CMP(E x, E y) { return x.z < y.z; } int find(int x) { if (f[x] != x) f[x] = find(f[x]); return f[x]; } int main() { scanf("%d%d", &n, &m); for (rint i = 1; i <= n; ++i) scanf("%lld", &num[i]); for (rint i = 1, x, y; i <= m; ++i) { scanf("%d%d%lld", &e[i].x, &e[i].y, &e[i].z); addedge(e[i].x, e[i].y, e[i].z); addedge(e[i].y, e[i].x, e[i].z); } for (rint i = 1; i <= n; ++i) f[i] = i; sort(e + 1, e + m + 1, CMP); for (rint i = 1; i <= m; ++i) { int fx = find(e[i].x), fy = find(e[i].y); if (fx == fy) { if (num[fx] < e[i].z) ans++, b[fx]++; } else { f[fx] = fy, num[fy] += num[fx]; b[fy] += b[fx]; if (num[fy] >= e[i].z) { ans -= b[fy]; b[fy] = 0; } else { b[fy]++; ans++; } } } printf("%d\n", ans); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <set> #include <vector> #define MAXN 100005 #define INF 0x3f3f3f3f #define rint register int #define LL long long #define LD long double using namespace std; int n, m, cnt, ans, head[MAXN], f[MAXN], b[MAXN]; LL num[MAXN]; struct Edge { int next, to; LL dis; } edge[MAXN * 2]; struct E { int x, y; LL z; } e[MAXN]; void addedge(int from, int to, LL dis) { edge[++cnt].next = head[from]; edge[cnt].to = to; edge[cnt].dis = dis; head[from] = cnt; } bool CMP(E x, E y) { return x.z < y.z; } int find(int x) { if (f[x] != x) f[x] = find(f[x]); return f[x]; } int main() { scanf("%d%d", &n, &m); for (rint i = 1; i <= n; ++i) scanf("%lld", &num[i]); for (rint i = 1, x, y; i <= m; ++i) { scanf("%d%d%lld", &e[i].x, &e[i].y, &e[i].z); addedge(e[i].x, e[i].y, e[i].z); addedge(e[i].y, e[i].x, e[i].z); } for (rint i = 1; i <= n; ++i) f[i] = i; sort(e + 1, e + m + 1, CMP); for (rint i = 1; i <= m; ++i) { int fx = find(e[i].x), fy = find(e[i].y); if (fx == fy) { if (num[fx] < e[i].z) ans++, b[fx]++; } else { f[fx] = fy, num[fy] += num[fx]; b[fy] += b[fx]; if (num[fy] >= e[i].z) { ans -= b[fy]; b[fy] = 0; } else { b[fy]++; ans++; } } } printf("%d\n", ans); return 0; }
replace
30
31
30
31
0
p03143
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <numeric> #include <tuple> #include <vector> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; std::vector<int> x(n); for (int i = 0; i < n; ++i) std::cin >> x[i]; std::vector<std::tuple<int, int, int>> ed(m); for (int i = 0; i < m; ++i) { std::cin >> std::get<1>(ed[i]) >> std::get<2>(ed[i]) >> std::get<0>(ed[i]); std::get<1>(ed[i])--; std::get<2>(ed[i])--; } std::sort(ed.begin(), ed.end()); std::vector<int> uf(n), uf2(n); std::vector<int64_t> sum(x.begin(), x.end()); std::vector<std::vector<std::pair<int, int>>> mg(n); std::iota(uf.begin(), uf.end(), 0); std::iota(uf2.begin(), uf2.end(), 0); std::function<int(int, std::vector<int> &)> Find = [&](int x, std::vector<int> &uf) { if (x == uf[x]) return x; return uf[x] = Find(uf[x], uf); }; std::function<int(int, int, std::pair<int, int>)> Merge = [&](int x, int y, std::pair<int, int> e) { assert((x = Find(x, uf)) != (y = Find(y, uf))); if (mg[x].size() > mg[y].size()) std::swap(x, y); uf[x] = y; sum[y] += sum[x]; mg[y].push_back(e); mg[y].insert(mg[y].end(), mg[x].begin(), mg[x].end()); return y; }; std::vector<std::tuple<int, int, int>> others; for (auto &e : ed) { int w, u, v; std::tie(w, u, v) = e; if (Find(u, uf) == Find(v, uf)) continue; u = Merge(u, v, std::make_pair(u, v)); if (sum[u] >= w) { for (auto e : mg[u]) { uf2[Find(e.first, uf2)] = Find(e.second, uf2); } } } std::vector<int64_t> sum2(n); for (int i = 0; i < n; ++i) sum2[Find(i, uf2)] += x[i]; int ans = 0; for (auto &e : ed) { int w, u, v; std::tie(w, u, v) = e; if (Find(u, uf2) == Find(v, uf2)) { if (sum2[Find(u, uf2)] >= w) ++ans; } } std::cout << m - ans << "\n"; return 0; }
#include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <numeric> #include <tuple> #include <vector> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; std::vector<int> x(n); for (int i = 0; i < n; ++i) std::cin >> x[i]; std::vector<std::tuple<int, int, int>> ed(m); for (int i = 0; i < m; ++i) { std::cin >> std::get<1>(ed[i]) >> std::get<2>(ed[i]) >> std::get<0>(ed[i]); std::get<1>(ed[i])--; std::get<2>(ed[i])--; } std::sort(ed.begin(), ed.end()); std::vector<int> uf(n), uf2(n); std::vector<int64_t> sum(x.begin(), x.end()); std::vector<std::vector<std::pair<int, int>>> mg(n); std::iota(uf.begin(), uf.end(), 0); std::iota(uf2.begin(), uf2.end(), 0); std::function<int(int, std::vector<int> &)> Find = [&](int x, std::vector<int> &uf) { if (x == uf[x]) return x; return uf[x] = Find(uf[x], uf); }; std::function<int(int, int, std::pair<int, int>)> Merge = [&](int x, int y, std::pair<int, int> e) { assert((x = Find(x, uf)) != (y = Find(y, uf))); if (mg[x].size() > mg[y].size()) std::swap(x, y); uf[x] = y; sum[y] += sum[x]; mg[y].push_back(e); mg[y].insert(mg[y].end(), mg[x].begin(), mg[x].end()); return y; }; std::vector<std::tuple<int, int, int>> others; for (auto &e : ed) { int w, u, v; std::tie(w, u, v) = e; if (Find(u, uf) == Find(v, uf)) continue; u = Merge(u, v, std::make_pair(u, v)); if (sum[u] >= w) { for (auto e : mg[u]) { uf2[Find(e.first, uf2)] = Find(e.second, uf2); } mg[u].clear(); } } std::vector<int64_t> sum2(n); for (int i = 0; i < n; ++i) sum2[Find(i, uf2)] += x[i]; int ans = 0; for (auto &e : ed) { int w, u, v; std::tie(w, u, v) = e; if (Find(u, uf2) == Find(v, uf2)) { if (sum2[Find(u, uf2)] >= w) ++ans; } } std::cout << m - ans << "\n"; return 0; }
insert
59
59
59
60
TLE