QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346182#6526. CanvasTeracWA 7ms70424kbC++144.5kb2024-03-07 22:06:122024-03-07 22:06:13

Judging History

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

  • [2024-03-07 22:06:13]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:70424kb
  • [2024-03-07 22:06:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

namespace IO {
	#if ONLINE_JUDGE
	#define getc() (IS == IT && (IT = (IS = ibuf) + fread(ibuf, 1, IL, stdin), IS == IT) ? EOF : *IS++)
	#else
	#define getc() getchar()
	#endif
	const int IL = 1 << 21, OL = 1 << 21;
	int olen = 0;
	char ibuf[IL], *IS = ibuf, *IT = ibuf, obuf[OL];
	inline int read() {
		register char ch = getc(); register int x = 0, f = 1;
		while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getc(); }
		while(isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
		return x * f;
	}
	inline double readdb() {
		register char ch = getc(); register double x = 0, f = 1;
		while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getc(); }
		while(isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
		if(ch == '.') {
			register double b = 0.1;
			ch = getc();
			while(isdigit(ch)) x += (ch - 48) * b, b *= 0.1, ch = getc();
		}
		return x * f;
	}
	inline int readstr(char *s) {
		register char ch = getc(); register int len = 0;
		while(!isalpha(ch)) ch = getc();
		while(isalpha(ch)) s[++len] = ch, ch = getc();
		return len;
	}
	inline void flush() { fwrite(obuf, 1, olen, stdout); olen = 0; }
	inline void putc(register char ch) { obuf[olen++] = ch; }
	template<class T>
	inline void write(register T x) {
		if(x < 0) obuf[olen++] = '-', x = -x;
		if(x > 9) write(x / 10);
		obuf[olen++] = x % 10 + 48;
	}
} using namespace IO;
const int N = 5e5 + 10;
int n, m;
map<int, int> mp[N];
struct query {
	int l, r, x, y;
} q[N];
int ans[N];
bool vis[N];
vector<int> e[N];
bool ist[N];
stack<int> st;
int bel[N], dfn[N], low[N], tim;
vector<int> vc[N];
int cnt;
void dfs(int u) {
	low[u] = dfn[u] = ++tim;
	ist[u] = 1;
	st.push(u);
	for(auto v : e[u]) {
		if(!dfn[v]) {
			dfs(v);
			low[u] = min(low[u], low[v]);
		}
		else if(ist[v]) low[u] = min(low[u], dfn[v]);
	}
	if(low[u] == dfn[u]) {
		int v;
		++cnt;
		do {
			v = st.top();
			st.pop();
			ist[v] = 0;
			vc[cnt].push_back(v);
			bel[v] = cnt; 
		} while(v != u);
	}
}
#define pii pair<int, int>
#define fir first
#define sec second
vector<pii> e2[N];
queue<int> Q;
int in[N], val[N];
bool vis2[N];
void MAIN() {
	n = read(), m = read();
	for(int i = 1; i <= n; i++)
		vis2[i] = 0, mp[i].clear(), e[i].clear(), e2[i].clear(), vc[i].clear(), in[i] = val[i] = ist[i] = dfn[i] = low[i] = 0;
	for(int i = 1; i <= m; i++)
		vis[i] = 0, q[i].l = read(), q[i].x = read(), q[i].r = read(), q[i].y = read();
	int tot1 = 1, tot2 = m;
	cnt = tim = 0;
	for(int i = 1; i <= m; i++) {
		if(q[i].x == 1 && q[i].y == 1)
			ans[tot2--] = i, vis[i] = 1;
		if(q[i].x == 2 && q[i].y == 2)
			ans[tot1++] = i, vis[i] = 1, vis2[q[i].x] = vis2[q[i].y] = 1;
	}
	for(int i = 1; i <= m; i++) {
		if(q[i].x + q[i].y == 3) {
			if(q[i].x == 1) {
//				cout << "edge:" << q[i].l << " " << q[i].r << endl;
				if(!mp[q[i].l].count(q[i].r)) e[q[i].l].push_back(q[i].r), mp[q[i].l][q[i].r] = i;
			}
			else {
//				cout << "edge:" << q[i].r << " " << q[i].l << endl;
				if(!mp[q[i].r].count(q[i].l)) e[q[i].r].push_back(q[i].l), mp[q[i].r][q[i].l] = i;
			}
		}
	}
	for(int i = 1; i <= n; i++)
		if(!dfn[i]) dfs(i);
	for(int i = 1; i <= n; i++)
		for(auto x : e[i])
			if(bel[i] != bel[x])
				e2[bel[i]].push_back({i, x}), in[bel[x]]++;
	for(int i = 1; i <= cnt; i++)
		if(!in[i]) Q.push(i);
	while(!Q.empty()) {
		int u = Q.front();
		Q.pop();
		int id = 0;
//		for(auto x :vc[u])
//			cout <<"x:"<<x<<endl;
		for(int i = 0; i < vc[u].size(); i++)
			if(vis2[vc[u][i]]) { id = i; break; }
		for(int i = id, j = 0; j < vc[u].size() - 1; i = (i + vc[u].size() - 1) % vc[u].size(), j++) {
			int x = mp[vc[u][i]][vc[u][(i + vc[u].size() - 1) % vc[u].size()]];
			vis2[vc[u][i]] = vis2[vc[u][(i + vc[u].size() - 1) % vc[u].size()]] = 1;
			ans[tot1++] = x;
//			cout << x << endl;
			vis[x] = 1;
		}
		for(auto x : e2[u]) {
			in[bel[x.sec]]--;
			if(!in[bel[x.sec]]) Q.push(bel[x.sec]);
			int t = mp[x.fir][x.sec];
			vis2[x.sec] = 1;
			ans[tot1++] = t;
			vis[t] = 1;
		}
	}
	for(int i = 1; i <= m; i++)
		if(!vis[i]) ans[tot1++] = i;
	reverse(ans + 1, ans + 1 + m);
	for(int i = 1; i <= m; i++)
		val[q[ans[i]].l] = q[ans[i]].x, val[q[ans[i]].r] = q[ans[i]].y;
	int sum = 0;
	for(int i = 1; i <= n; i++)
		sum += val[i];
	printf("%d\n", sum);
	for(int i = 1; i <= m; i++)
		printf("%d ", ans[i]);
	puts("");
}
int main() {
	int T = read();
	while(T--) MAIN();
	return 0;
}
/*
1
4 4
1 1 2 2
2 1 3 2
3 1 4 2
4 1 1 2
*/ 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 70424kb

input:

2
4 4
1 1 2 2
3 2 4 1
1 2 3 2
2 1 4 1
4 2
3 2 4 1
1 2 3 1

output:

7
4 2 1 3 
5
2 1 

result:

ok Correct. (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 68176kb

input:

1
10 13
1 1 2 2
2 1 3 2
1 2 3 1
3 1 4 2
4 1 5 2
5 1 6 2
4 2 6 1
7 1 8 2
8 1 9 2
7 2 9 1
5 2 9 1
8 2 10 2
1 1 10 1

output:

18
13 9 5 1 7 6 11 8 10 4 3 2 12 

result:

wrong answer Jury has better answer. Participant 18, jury 19 (test case 1)