QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578862#9349. Exchanging GiftsEricWRE 0ms3844kbC++233.8kb2024-09-20 21:58:452024-09-20 21:58:46

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 21:58:46]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3844kb
  • [2024-09-20 21:58:45]
  • 提交

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;
	bool flag = false;
	for (int i = 0;i < n;i++) {
		int opt; read(opt);
		if (opt == 1) {
			if (i == n - 1) flag = true;
			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());
	if (flag) {
		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 ;
	}
	queue<int> q;
	q.emplace(n - 1); dp[n - 1] = 1;
	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());
	assert(valid.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: 3844kb

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
Runtime Error

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:


result: