玄关,求调
查看原帖
玄关,求调
712068
Snow_Dog7楼主2024/10/21 08:37
#include <bits/stdc++.h>
using namespace std;

struct QuQ{
	int x,c,y,d;
}cz[114514];
int T;
int n,m;
vector <pair<int,int>> tu[114514];
int tot;
int dfn[114514],low[114514],cnt;
int inst[114514],id[114514],dx[114514],scc;
bool v[114514],two[114514];
int ans[114514],dep[114514];
stack <int> st;
vector <int> q,p;

void tarjan(int x) {
	dfn[x] = low[x] = ++cnt;
	st.push(x);
	inst[x] = 1;
	int siz = tu[x].size();
	for (int i = 0;i < siz;++i) {
		int y = tu[x][i].first;
		if (!dfn[y]) {
			tarjan(y);
			low[x] = min(low[x],low[y]);
		}
		else {
			if (inst[y]) low[x] = min(low[x],dfn[y]);
		}
	}
	if (dfn[x] == low[x]) {
		int y;++scc;
		do {
			y = st.top();
			st.pop();
			inst[y] = 0;
			id[y] = scc;
			++dx[scc];
		} while (y != x);
	}
}

void DFS(int x) {
    v[x] = 1;
    int siz = tu[x].size();
    for(int i = 0;i < siz;++i) {
    	int y = tu[x][i].first,z = tu[x][i].second;
        q.push_back(z);
        if(!v[y]) DFS(y);
    }
}

int main ( ) {
	cin >> T ;
	while (T--) {
		cin >> n >> m ;
		for (int i = 1;i <= m;++i) {
			cin >> cz[i].x >> cz[i].c >> cz[i].y >> cz[i].d ;
			if (cz[i].c == 2 && cz[i].d == 2) {
				q.push_back(i);
				two[cz[i].x] = two[cz[i].y] = 1;
			}
			else if (cz[i].c == 1 && cz[i].d == 1) {
				p.push_back(i);
			}
			else {
				if (cz[i].c < cz[i].d) tu[cz[i].x].push_back({cz[i].y,i});
				else tu[cz[i].y].push_back({cz[i].x,i});
			}
		}
		for (int i = 1;i <= n;++i) {
			if (!dfn[i]) tarjan(i);
		}
		
		for(int i = 1;i <= n;++i){
			int siz = tu[i].size();
			for(int j = 0;j < siz;++j) {
				if(id[i] != id[j]) ++dep[id[j]];
			}
		}
		
		for (int i = 1;i <= n;++i) {
			if (!dep[id[i]] && !v[i] && two[i]) DFS(i);
		}
		for (int i = 1;i <= n;++i) {
			if (!dep[id[i]] && !v[i]) DFS(i);
		}
		int siz1 = p.size(),siz2 = q.size();
		for (int i = siz1 - 1;i >= 0;--i) {
			ans[cz[p[i]].x] = cz[p[i]].c;
			ans[cz[p[i]].y] = cz[p[i]].d;
		}
		for (int i = siz2 - 1;i >= 0;--i) {
			ans[cz[q[i]].x] = cz[q[i]].c;
			ans[cz[q[i]].y] = cz[q[i]].d;
		}
		for (int i = 1;i <= n;++i) {
			tot += ans[i];
		}
		cout << tot << '\n' ;
		for (int i = siz1 - 1;i >= 0;--i) {
			cout << p[i] << ' ' ;
		}
		for (int i = siz2 - 1;i >= 0;--i) {
			cout << q[i] << ' ' ;
		}
		cout << '\n' ;
		for (int i = 1;i <= n;++i) {
			dfn[i] = low[i] = id[i] = 0;
			ans[i] = inst[i] = v[i] = 0;
			dx[i] = ans[i] = two[i] = 0;
			dep[i] = 0;
			tu[i].clear();
		}
		q.clear();
		p.clear();
		tot = cnt = scc = 0;
	}
}

太菜了,样例都过不了。

目测应该不是tarjan或建图的问题

2024/10/21 08:37
加载中...