• E. Monsters


    Problem - 1810E - Codeforces

    思路:我们总结一下题意,能够得到这个题其实就是让我们从某个0开始搜索,然后看看是否可以遍历所有得节点,那么如果采用暴力得话那就是n^2logn,因为我们遍历一次使用优先队列得话是nlogn的,但是通过总结我们发现了一个性质,每个点被遍历的次数不超过logn次,因为假设在我们现在正在遍历以u开始的点,那么假设在某一次遍历中遍历到了一个节点t,这个节点已经在以v开始的节点中被遍历过了,那么如果我现在能够遍历到节点t,那么就代表我可以遍历所有v能够遍历到的点,那么现在的数量起码就是cnt[v]*2,那么我们知道了,每次重复遍历一个节点,那么能够遍历到的点就会乘以2,那么每个点最多只会被遍历logn次

    1. // Problem: E. Monsters
    2. // Contest: Codeforces - CodeTON Round 4 (Div. 1 + Div. 2, Rated, Prizes!)
    3. // URL: https://codeforces.com/problemset/problem/1810/E?mobile=false
    4. // Memory Limit: 256 MB
    5. // Time Limit: 2000 ms
    6. #include
    7. #include
    8. #include
    9. #define fi first
    10. #define se second
    11. #define i128 __int128
    12. using namespace std;
    13. typedef long long ll;
    14. typedef double db;
    15. typedef pair<int,int> PII;
    16. const double eps=1e-7;
    17. const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
    18. const long long int llINF=0x3f3f3f3f3f3f3f3f;
    19. inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
    20. while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
    21. inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
    22. inline void write(ll x,char ch) {write(x);putchar(ch);}
    23. void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
    24. bool cmp0(int a,int b) {return a>b;}
    25. template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
    26. template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
    27. void hack() {printf("\n----------------------------------\n");}
    28. int T,hackT;
    29. int n,m,k;
    30. int cost[N];
    31. int h[N],e[M],ne[M],idx;
    32. bool st[N];
    33. int stt[N];
    34. void add(int a,int b) {
    35. e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    36. }
    37. bool bfs(int u) {
    38. priority_queue,greater > q;
    39. q.push({cost[u],u});
    40. int res=0;
    41. while(q.size()) {
    42. auto it=q.top();
    43. q.pop();
    44. int ver=it.se;
    45. if(it.fi>res) break;
    46. if(stt[ver]==u) continue;
    47. st[ver]=true;
    48. stt[ver]=u;
    49. res++;
    50. for(int i=h[ver];i!=-1;i=ne[i]) {
    51. int j=e[i];
    52. q.push({cost[j],j});
    53. }
    54. }
    55. return res==n;
    56. }
    57. void solve() {
    58. n=read(),m=read();
    59. memset(st,false,sizeof(bool)*(n+4));
    60. memset(stt,0,sizeof(int)*(n+4));
    61. for(int i=1;i<=n;i++) cost[i]=read();
    62. memset(h,-1,sizeof(int)*(n+4));
    63. idx=0;
    64. while(m--) {
    65. int a=read(),b=read();
    66. add(a,b),add(b,a);
    67. }
    68. bool flag=false;
    69. for(int i=1;i<=n;i++) {
    70. if(st[i]) continue;
    71. if(cost[i]!=0) continue;
    72. if(bfs(i)) {
    73. flag=true;
    74. }
    75. }
    76. if(flag) printf("YES\n");
    77. else printf("NO\n");
    78. }
    79. int main() {
    80. // init();
    81. // stin();
    82. // ios::sync_with_stdio(false);
    83. scanf("%d",&T);
    84. // T=1;
    85. while(T--) hackT++,solve();
    86. return 0;
    87. }

  • 相关阅读:
    cf #832 Div.2(A-D)
    高级查询
    07【保姆级】-GO语言的程序流程控制【if switch for while 】
    python每日一练(4)
    制造企业如何优化物料控制?
    【mysql学习笔记25】sql语句优化
    电动汽车有序无序充放电的优化调度附matlab代码
    在C#中使用 NLog 库进行日志记录
    【数据结构】链表中二级指针的应用
    Pan-cancer image-based detection of clinically actionable genetic alternations
  • 原文地址:https://blog.csdn.net/zzzyyzz_/article/details/133687971