# [luogu5008]逛庭院

# include <bits/stdc++.h>
# define Ri register int
# define for1(i,a,b) for(Ri i(a);i<=b;++i)
# define for2(i,a,b) for(Ri i(a);i>=b;--i)

using namespace std;

{
int w = 0,x = 0;
char ch = 0;
while (!isdigit(ch)) { w |= ch =='-'; ch = getchar();}
while (isdigit(ch)) { x = (x<<1) + (x<<3) + (ch ^ 48); ch = getchar(); }
return w ? -x : x;
}

const int Maxm = 2000004;
const int Maxn = 5000004;

int Nedge, n, m, k;

struct node{
int v ,id ,ind ;
}a[5000004];

bool cmp (node a,node b)
{
return a.v > b.v;
}

int main()
{
for1(i ,1 ,n ) a[i].v = read(),a[i].id = i;
for1(i ,1 ,m )
{
a[v].ind ++;
}
sort (a + 1 , a + 1 + n , cmp);
int cnt = 0 , ans = 0;
for1(i ,1 ,n)
{
if (a[i].ind == 0) continue;
else
{
ans += a[i].v;
cnt ++;
if (cnt == k) break;
}
}
printf ("%d\n", ans);
return 0;
}

## 为什么

inline void tarjan(int u)
{
dfn[u] = low[u] = ++ dep;
vis[u] = 1;
S[top++] = u;
for (int i = head[u]; i != -1; i = edge[i].next )
{
int v = edge[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = Min(low[u] ,low[v]);
}
else if (vis[v]) low[u] = Min(low[u] ,low[v]);
}
int j;
if (dfn[u] == low[u])
{
sum ++;
do
{
j = S[ -- top];
belong[j] = sum ;
vis [j] = 0;
}while (u != j) ;
}
}


## 解释

### 那么还有一个问题，也就是如果是一个节点的缩点？

# include <bits/stdc++.h>
# define Ri register int
# define for1(i,a,b) for(Ri i(a);i<=b;++i)
# define for2(i,a,b) for(Ri i(a);i>=b;--i)
# define ms(a,b) memset(a,b,sizeof(a))

using namespace std;

typedef long long LL;

const int M = 2000005;

struct Edge{
int to ,next;
}edge[M];

int dfn[M], vis[M], low[M], S[M], head[M] ,belong[M] ,ind[M];
int dep, top, sum , n ,m ,k ,Nedge;

struct node{
int v ,id ;
}a[M];

{
int w = 0,x = 0;
char ch = 0;
while (!isdigit(ch))
{
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch))
{
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return w ? -x : x ;
}

inline int Min(int n,int m) //三目取min
{
return n < m ? n : m;
}

inline void Add_Edge(int u ,int v) //链式前向星
{
edge[Nedge] = (Edge) {v ,head[u]} ;
}

inline void tarjan(int u) //tarjan缩点模板
{
dfn[u] = low[u] = ++ dep;
vis[u] = 1;
S[top++] = u;
for (int i = head[u]; i != -1; i = edge[i].next )
{
int v = edge[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = Min(low[u] ,low[v]);
}
else if (vis[v]) low[u] = Min(low[u] ,low[v]);
}
int j;
if (dfn[u] == low[u])
{
sum ++;
do
{
j = S[ -- top];
belong[j] = sum ;
vis [j] = 0;
}while (u != j) ;
}
}

inline bool cmp1(node a,node b) //从小到大排序
{
return a.v < b.v;
}

inline bool cmp2(node a,node b) //从大到小排序
{
return a.v > b.v;
}

int main()
{
ms(dfn ,0);
ms(vis ,0);
ms(belong ,0);
sum = 0,dep = 0,top = 0;
for1(i ,1 ,n) a[i].v = read(),a[i].id = i;
for1(i ,1 ,m)
{
}
for1(i ,1 ,n)
{
if (!dfn[i]) tarjan(i); // 缩一波点
}
for1(i ,1 ,n)
{
for (int j = head[i]; j != -1; j = edge[j].next)
{
int v = edge[j].to;
if ( belong[i] != belong[v] ) ind[belong[v]] ++;
}
}//统计当前缩完点后的每个点的入度
sort(a + 1, a + 1 + n ,cmp1);
for1(i ,1 ,n)
{
if (ind[belong[a[i].id]] == 0)
{
a[i].v = 0;
ind[belong[a[i].id]] = 1;
}
}//删去一个联通块中权值最小的点
sort(a + 1 , a + 1 + n ,cmp2);
LL ans = 0, cnt = 0;
for1(i ,1 ,n) //计算我们的答案
{
ans += a[i].v;
cnt ++ ;
if (cnt == k) break;
}
printf ("%lld\n", ans);
return 0;
}
posted @ 2019-03-14 10:56 chhokmah 阅读(...) 评论(...) 编辑 收藏