30求条
查看原帖
30求条
690243
houluyu楼主2024/11/10 17:41
#include<bits/stdc++.h>
using namespace std;

const int N = 2005, M = 10005, inf = 0x7fffffff;
int n, m;
int k[N];//最早第几个走 按从小到大排
vector<vector<int> > g(N), g1(N);//g[i][j]表示
int in[N];
struct dat{
	int u;
	friend bool operator<(dat xx, dat yy)
	{
		return k[xx.u] < k[yy.u];
	}
};
priority_queue<dat> q, tmp;//tmp是暂时不行的  都是小的尽可能考前
int in_[N];
int ans[N];
int kk[N];

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++)
	{
		cin >> k[i];//在这之后飞
		kk[i] = k[i];
	}
	for(int i = 1; i <= m; i ++)
	{
		int u, v;
		cin >> u >> v;
		g[v].push_back(u);//必须在这之后
		in[u] ++;
	}
	for(int i = 1; i <= n; i ++)
	{
		if(!in[i])
		{
			q.push({i});
		}
		in_[i] = in[i];
	}
	stack<int> st;
	int tot = 0;
	while(!q.empty())
	{
		dat t = q.top();
		q.pop();
		tot ++;
        st.push(t.u);
		for(int i = 0; i < g[t.u].size(); i ++)
		{
			int v = g[t.u][i];
			in[v] --;
			if(!in[v])
			{
				q.push({v});
			}
		}
	}
	while(!st.empty())
	{
		cout << st.top() << ' ';
		st.pop();
	}
	cout << endl;
	for(int i = 1; i <= n; i ++)//i尽量晚飞
	{
		while(!q.empty())q.pop();
		while(!tmp.empty())tmp.pop();
		for(int j = 1; j <= n; j ++)
		{
			k[j] = n - kk[j] + 1;//最早啥时候
			in[j] = in_[j];
			if(!in[j])
			{
				if(k[j] > 1)tmp.push({j});
				else q.push({j});
			}
		}
		int tot = 1;
		while(!q.empty())
		{
			dat t = q.top();
			q.pop();
			if(t.u == i && !q.empty())
			{
				t = q.top();
				q.pop();
				q.push({i});
			}
			if(t.u == i)
			{
				ans[i] = tot;//最晚啥时候走  第几个
				break;
			}
			for(int i = 0; i < g[t.u].size(); i ++)
			{
				int v = g[t.u][i];
				in[v] --;
				if(!in[v])
				{
					if(k[v] > tot)
					{
						tmp.push({v});
					}
					else
					q.push({v});
				}
			}
			tot ++;//出来几个了
			while(!tmp.empty() && k[tmp.top().u] <= tot)
			{
//				cout << 4456 << endl;
				dat t = tmp.top();
				tmp.pop();
				q.push(t);
			}
			tot = min(tot, n);
		}
	}
	for(int i = 1; i <= n; i ++)
	{
		cout << n - ans[i] + 1 << ' ';
	}
	cout << endl;
	return 0;
}



2024/11/10 17:41
加载中...