QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578837 | #9349. Exchanging Gifts | EricW | WA | 130ms | 3756kb | C++23 | 3.8kb | 2024-09-20 21:49:16 | 2024-09-20 21:49:16 |
Judging History
answer
// Program written by EricWu ~~~
#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
#define gc() getchar()
template <class T> inline void read(T &x) {
T f = 1; x = 0; static char c = gc();
for (; !isdigit(c); c = gc()) if (c == '-') f = -f;
for (; isdigit(c); c = gc()) x = x * 10 + (c & 15);
if (f == -1) x = -x;
}
inline void readstr(char *s) {
do *s = gc(); while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc(); while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0; return;
}
inline void readch(char&x) { while (isspace(x = gc())); }
char pf[100000], *o1 = pf, *o2 = pf + 100000;
#define ot(x) (o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf) ++= x : *o1 ++= x)
template <class T>
inline void println(T x, char c = '\n') {
if (x < 0) ot(45), x = -x;
static char s[15], *b; b = s;
if (!x) *b ++= 48;
for (; x; * b ++= x % 10 + 48, x /= 10);
for (; b-- != s; ot(*b)); ot(c);
}
template <class T> inline void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <class T> inline void writeln(T x, char c = '\n') { write(x); putchar(c); }
template <class T> inline void chkmax(T &x, const T y) { x > y ? x = x : x = y; }
template <class T> inline void chkmin(T &x, const T y) { x < y ? x = x : x = y; }
#define Ms(arr, opt) memset(arr, opt, sizeof(arr))
#define Mp(x, y) make_pair(x, y)
#define endl '\n'
typedef long long ll;
typedef pair <int, int> pii;
typedef array<int, 3> piii;
using i64 = long long;
const int inf = 1e9;
void solve() {
int n; read(n);
vector adj(n, vector<int>());
vector<int> ind(n, 0), dp(n, 0);
vector cnt(n, vector<int>());
vector<int> sorted;
set<int> valid;
for (int i = 0;i < n;i++) {
int opt; read(opt);
if (opt == 1) {
int k; read(k); cnt[i].resize(k + 1);
cnt[i][0] = k;
for (int j = 1;j <= k;j++)
read(cnt[i][j]), sorted.emplace_back(cnt[i][j]);
} else {
int x, y; read(x), read(y); x --, y --;
adj[i].push_back(x), adj[i].push_back(y);
ind[x] ++, ind[y] ++;
}
}
sort(sorted.begin(), sorted.end());
sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
queue<int> q;
if (ind[n - 1] == 0) q.emplace(n - 1), dp[n - 1] = 1;
else {
// cout << "yes\n";
int siz = cnt[n - 1][0];
vector<int> s(siz);
for (int j = 1;j <= siz;j++) {
auto idx = lower_bound(sorted.begin(), sorted.end(), cnt[n - 1][j]) - sorted.begin();
s[idx] += 1;
}
sort(s.begin(), s.end(), [](auto &x, auto &y){
return x > y;
});
i64 ans = 0, last = 0;
for (int i = 0;i < s.size();i++) {
if (s[i] >= last) ans += (last * 2), last = s[i] - last;
else ans += (s[i] * 2), last -= s[i];
}
writeln(ans);
return ;
}
while (q.size()) {
auto u = q.front(); q.pop();
if (adj[u].size() == 0) valid.insert(u);
for (auto &v : adj[u]) {
dp[v] += dp[u];
ind[v] --;
if (ind[v] == 0) q.emplace(v);
}
}
vector<int> s(sorted.size());
for (auto &it : valid) {
int siz = cnt[it][0];
for (int i = 1;i <= siz;i++) {
auto idx = lower_bound(sorted.begin(), sorted.end(), cnt[it][i]) - sorted.begin();
s[idx] += dp[it];
}
}
sort(s.begin(), s.end(), [](auto &x, auto &y) {
return x > y;
});
i64 ans = 0, last = 0;
for (int i = 0;i < s.size();i++) {
if (s[i] >= last) ans += (last * 2), last = s[i] - last;
else ans += (s[i] * 2), last -= s[i];
}
writeln(ans);
}
int main() {
int T = 1; read(T);
while (T--) solve();
// system("pause");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
input:
2 1 1 5 3 3 2 1 3 3 1 3 3 3 2 1 4 2 2 3 3 2 1 2
output:
4 6
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 130ms
memory: 3756kb
input:
10000 100 1 30 371028678 371028678 371028678 716418076 398221499 591504380 398221499 398221499 591504380 777141390 398221499 591504380 591504380 777141390 287847807 716418076 777141390 716418076 777141390 287847807 287847807 287847807 371028678 371028678 398221499 777141390 371028678 6827702 6827702...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
wrong answer 1st lines differ - expected: '700', found: '0'