生活随笔
收集整理的這篇文章主要介紹了
NWERC 2018——B.Brexit Negotiations
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Brexit Negotiations
有向無環圖,很容易想到拓撲排序,很明顯我們需要把權值大的點放在最前面,然后就想到搞個優先隊列,每次拓撲排序的時候出權值大的點,但是發現答案是不對的。
正向建圖拓撲排序能夠保證小的點一定最后取到,而如果反向建圖每次取最小的可以保證最大的點最后被取到,顯然我們需要的是最大的點放在最前面因此需要方向建圖。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std
;
typedef long long ll
;
typedef pair
<int,int> pii
;
const int mod
=1e9+7;
const int N
=400010;
int h
[N
],e
[N
],ne
[N
],idx
;
int d
[N
];
int w
[N
];
int n
,res
;
void add(int a
,int b
)
{e
[idx
]=b
;ne
[idx
]=h
[a
];h
[a
]=idx
++;
}
void topsort()
{priority_queue
<pii
,vector
<pii
>,greater
<pii
> >q
;for(int i
=1;i
<=n
;i
++)if(!d
[i
]) q
.push({w
[i
],i
});int tt
=n
;while(q
.size()){int t
=q
.top().second
;q
.pop();tt
--;res
=max(res
,w
[t
]+tt
);for(int i
=h
[t
];i
!=-1;i
=ne
[i
]){int j
=e
[i
];if(--d
[j
]==0) q
.push({w
[j
],j
});}}
}
int main()
{int T
=1;for(int ca
=1;ca
<=T
;ca
++){cin
>>n
;memset(h
,-1,sizeof h
);for(int i
=1;i
<=n
;i
++){int cnt
;cin
>>w
[i
]>>cnt
;while(cnt
--){int a
;cin
>>a
;add(i
,a
);d
[a
]++;}}topsort();cout
<<res
<<'\n';}return 0;
}
總結:雖然有時候很難理性的分析出結果,但是對于貪心需要大膽的取猜結論多嘗試?做了很長時間沒有結果如果當時多試試可能就能碰出答案了,雖然很玄hhh
總結
以上是生活随笔為你收集整理的NWERC 2018——B.Brexit Negotiations的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。