QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#792222#6127. Kawa ExamSin_WattWA 3ms12204kbC++144.5kb2024-11-29 07:30:292024-11-29 07:30:29

Judging History

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

  • [2024-11-29 07:30:29]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:12204kb
  • [2024-11-29 07:30:29]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long lnt;

const int N = 1e5 + 7;

int n, m;
int a[N];
int va[N], vn;
int *am[N];

struct E {
	int to, ne;
} e[N << 1];
int idx = 1;
int h[N];

inline void add(int x, int y) {
	e[ ++ idx] = (E){y, h[x]}; h[x] = idx;
	e[ ++ idx] = (E){x, h[y]}; h[y] = idx;
}

int dnt;
int dfn[N];
int low[N];
int sta[N], stt;

int frm[N];
int scc;
vector<int> hav[N];

void tarjan(int u, int fe) {
	u[dfn] = u[low] = ++ dnt;
	stt[sta] = u; ++ stt;
	for (int i = h[u]; i; i = e[i].ne) {
		if (i == fe) continue;
		int v = e[i].to;
		if (v[dfn]) {
			u[low] = min(u[low], v[dfn]);
		} else {
			tarjan(v, i ^ 1);
			u[low] = min(u[low], v[low]);
		}
	}
	if (u[low] == u[dfn]) {
		++ scc;
		do {
			int v = sta[ -- stt];
			frm[v] = scc;
			scc[hav].push_back(v);
		} while (sta[stt] != u);
	}
}

struct Tree {
	int val[N << 2];
	
	#define ls (u << 1)
	#define rs (u << 1 | 1)
	#define all u = 1, int l = 1, int r = vn
	#define lq ls, l, mid
	#define rq rs, mid + 1, r
	
	void modify(int P, int V, int all) {
		if (l == r) {
			//cerr << "\t\t\t\t[Tree]  " << P << ' ' << V << '\n';
			u[val] += V;
			return;
		}
		int mid = (l + r) >> 1;
		if (P <= mid) {
			modify(P, V, lq);
		} else {
			modify(P, V, rq);
		}
		u[val] = max(ls[val], rs[val]);
	}
	
	int query() {
		return val[1];
	}
} T[2];

vector<pair<int, int>> G[N];
int siz[N];
int son[N];

void dfs1(int u, int fa) {
	u[siz] = u[hav].size();
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) continue;
		dfs1(v, u);
		u[siz] += v[siz];
		if (u[son][siz] < v[siz]) {
			u[son] = v;
		}
	}
}

void dfs2(int u, int fa, int t) {
	for (auto i : hav[u]) {
		T[0].modify(i[a], t);
	}
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) continue;
		dfs2(v, u, t);
	}
}

void dfs3(int u, int fa, int t) {
	for (auto i : hav[u]) {
		T[t    ].modify(i[a], -1);
		T[t ^ 1].modify(i[a], 1);
	}
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) continue;
		dfs3(v, u, t);
	}
}

int All;
int Ans;
int ans[N];

void dfs4(int u, int fa, int t) {
	//cerr << "dfs4 [in]\t" << u << ' ' << fa << ' ' << t << '\n';
	int fe = 0;
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa) {
			fe = pir.second;
			continue;
		} else if (v == u[son]) continue;
		dfs4(v, u, 1);
	}
	if (u[son]) {
		dfs4(u[son], u, 0);
	}
	for (auto i : hav[u]) {
		T[0].modify(i[a], -1);
		T[1].modify(i[a], 1);
	}
	for (auto pir : G[u]) {
		int v = pir.first;
		if (v == fa || v == u[son]) continue;
		dfs3(v, u, 0);
	}
	if (fe) {
		ans[fe] = T[0].query() + T[1].query() - All;
		//cerr << "     [get]\t" << u << ' ' << fe << ' ' << ans[fe] << '\n';
	}
	if (t) {
		for (auto i : hav[u]) {
			T[1].modify(i[a], -1);
			T[0].modify(i[a], 1);
		}
		for (auto pir : G[u]) {
			int v = pir.first;
			if (v == fa) continue;
			dfs3(v, u, 1);
		}
	}
	//cerr << "dfs4 [out]\t" << u << '\n';
}

inline void INIT() { }

inline void WORK() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++ i) {
		cin >> a[i];
		am[i] = a + i;
	}
	sort(am + 1, am + n + 1, [](const int *a, const int *b) -> bool {
		return *a < *b;
	});
	for (int i = 1; i <= n; ++ i) {
		if (va[vn] != *am[i]) {
			va[ ++ vn] = *am[i];
		}
		*am[i] = vn;
	}
	for (int i = 1; i <= m; ++ i) {
		int x, y;
		cin >> x >> y;
		add(x, y);
	}
	for (int i = 1; i <= n; ++ i) {
		if (!dfn[i]) {
			tarjan(i, 0);
		}
	}
	for (int u = 1; u <= n; ++ u) {
		for (int i = h[u]; i; i = e[i].ne) {
			int v = e[i].to;
			if (u[frm] != v[frm]) {
				u[frm][G].push_back({ v[frm], i >> 1 });
			}
		}
	}
	for (int i = 1; i <= scc; ++ i) {
		if (!siz[i]) {
			dfs1(i, 0);
			dfs2(i, 0, 1);
			Ans += All = T[0].query();
			dfs4(i, 0, 1);
			dfs2(i, 0, -1);
		}
	}
	for (int i = 1; i <= m; ++ i) {
		cout << Ans + ans[i] << ' ';
	}
	cout << '\n';
	
	vn = 0;
	idx = 0;
	dnt = 0;
	for (int i = 1; i <= n; ++ i) {
		h[i] = 0;
		dfn[i] = 0;
	}
	for (int i = 1; i <= m; ++ i) {
		ans[i] = 0;
	}
	for (int i = 1; i <= scc; ++ i) {
		G[i].clear();
		hav[i].clear();
		siz[i] = 0;
		son[i] = 0;
	}
	scc = 0;
	Ans = 0;
}

//#define filename ""

int main() {
	#ifdef filename
	freopen(filename ".in", "r", stdin);
	freopen(filename ".out", "w", stdout);
	#endif
	cin.tie(0); cout.tie(0);
	ios::sync_with_stdio(0);
	
	int Turn = 1;
	cin >> Turn;
	INIT();
	while (Turn -- ) {
		WORK();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 12204kb

input:

3
7 5
1 2 1 2 1 2 1
1 2
1 3
2 4
5 6
5 7
3 3
1 2 3
1 2
1 3
2 3
2 3
12345 54321
1 2
1 2
1 1

output:

6 5 5 5 4 
1 1 1 
1 1 1 

result:

wrong answer 1st lines differ - expected: '6 5 5 5 4', found: '6 5 5 5 4 '