1124: [POI2008]枪战Maf
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 500 Solved: 199
[ Submit][ Status][ Discuss]
Description
有n个人,每个人手里有一把手枪。一开始所有人都选定一个人瞄准(有可能瞄准自己)。然后他们按某个顺序开枪,且任意时刻只有一个人开枪。因此,对于不同的开枪顺序,最后死的人也不同。
Input
输入n人数<1000000 每个人的aim
Output
你要求最后死亡数目的最小和最大可能
Sample Input
8
2 3 2 2 6 7 8 5
2 3 2 2 6 7 8 5
Sample Output
3 5
HINT
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E6 + 10;
int a1,a2,n,cnt,dfs_clock,shoot[maxn],in[maxn],
belong[maxn],dfn[maxn],low[maxn],siz[maxn];
bool die[maxn],vis[maxn];
queue <int> Q;
stack <int> s;
void dfs(int x,int from)
{
dfn[x] = low[x] = ++dfs_clock;
s.push(x);
if (!dfn[shoot[x]]) {
dfs(shoot[x],x);
low[x] = min(low[x],low[shoot[x]]);
}
else if (!belong[shoot[x]]) low[x] = min(low[x],low[shoot[x]]);
if (low[x] >= dfn[x]) {
int now = 0; ++cnt;
for (;;) {
int top = s.top(); s.pop();
belong[top] = cnt;
++now;
if (top == x) break;
}
siz[cnt] = now;
}
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n; a2 = n;
for (int i = 1; i <= n; i++) scanf("%d",&shoot[i]),++in[shoot[i]];
for (int i = 1; i <= n; i++) {
if (!in[i]) Q.push(i),vis[i] = 1;
if (i == shoot[i]) ++a1,vis[i] = i;
}
while (!Q.empty()) {
int k = Q.front(); Q.pop();
if (vis[shoot[k]]) continue;
vis[shoot[k]] = 1;
++a1; int Nex = shoot[shoot[k]]; --in[Nex];
if (!in[Nex] && !vis[Nex]) vis[Nex] = 1,Q.push(Nex);
}
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
int now = 1; vis[i] = 1;
for (int j = shoot[i]; ; j = shoot[j]) {
if (vis[j]) break;
++now; vis[j] = 1;
}
a1 += (now+1)/2;
}
cout << a1 << ' ';
for (int i = 1; i <= n; i++)
if (!belong[i]) dfs(i,0);
for (int i = 1; i <= n; i++)
if (belong[i] != belong[shoot[i]] || i == shoot[i])
die[belong[shoot[i]]] = 1;
for (int i = 1; i <= cnt; i++)
if (!die[i]) --a2;
cout << a2;
return 0;
}