CODEFORCES-920E Connected components? Breadth Search to judge the deletion of duplicate points under large data of Unicom

Source: Internet
Author: User
Tags prev

Title Link: Https://cn.vjudge.net/problem/CodeForces-920E

Test instructions

Give a fill map, ask each unicom block has several elements, ascending order
Note Maxn=2e5, Maxm=2e10

Ideas

The amount of data is very large, which would have been a topic of research and
If you use and look up a set, maintain an element number up, but first offline mapping is a problem O (n^2)
If you think about it, BFS O (n) is a better choice.
Submitted to the tle, at that time writing the problem is not careful calculation of complexity, set check Edge + array weight, add up looks like O (nlogn+n), as to why with Set check edge, because the array is too large to check the edge
Finally check the puzzle, the weight is linked list deleted elements, equivalent to the actual deletion, the number of cycles greatly reduced
Okay, great.

Submission Process
Ce Header file
TLE1 Set<pair<int, int>> storage Edge
TLE2 Change to set
Wa1-3 Forget why WA ...
AC
Code
#include <set> #include <queue> #include <cstdio> #include <cstring> #include <algorithm> using namespace Std;const int maxn=2e5+20;int n, m, CNT, SIZE[MAXN], NEXT[MAXN], Prev[maxn];bool check[maxn];set<long l    ong> vis;void del (int x) {next[prev[x]]=next[x]; PREV[NEXT[X]]=PREV[X];}    int BFS (int x) {int ans=1; Queue<int> que;    Que.push (x); Check[x]=true;    Del (x);        while (Que.size ()) {int Node=que.front (); Que.pop (); for (int i=next[0]; i<=n; i=next[i]) if (!check[i] &&!vis.count ((Long Long) i*maxn+node) {Que.push (            i); Check[i]=true;            Del (i);        ans++; }}return ans;    int main (void) {int to, from;    scanf ("%d%d", &n, &m);        for (int i=0; i<m; i++) {scanf ("%d%d", &to, &from);        Vis.insert ((Long Long) to*maxn+from);    Vis.insert ((Long Long) from*maxn+to);    } for (int i=1; i<=n; i++) next[i]=i+1, prev[i]=i-1; Next[0]=1; Prev[n+1]=n;    int cnt=0;    for (int i=1; i<=n; i++) if (!check[i]) size[cnt++]=bfs (i);    Sort (size, size+cnt);    printf ("%d\n", CNT);    for (int i=0; i<cnt; i++) printf ("%d%c", size[i], "\ n" [i==cnt-1]); return 0;}
Time
Memory Length Lang submitted
343ms 16012kB 1067 GNU g++ 5.1.0 2018-07-23 17:51:00

CODEFORCES-920E Connected components? Breadth Search to judge the deletion of duplicate points under large data of Unicom

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.