Skip to content

Commit

Permalink
cf 920E: graph, amortized analysis, O(M log N)
Browse files Browse the repository at this point in the history
  • Loading branch information
prprprpony committed Mar 29, 2018
1 parent 91ef0e8 commit a221b4f
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions cf/920E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <bits/stdc++.h>
using namespace std;
// nichijou
#define REP(i,a,b) for (int i(a); i < (b); ++i)
#define RP(i,n) REP(i,0,n)
#define PER(i,a,b) for(int i((a)-1); i >= (b); --i)
#define PR(i,n) PER(i,n,0)
#define REP1(i,a,b) REP(i,a,(b)+1)
#define RP1(i,n) REP1(i,1,n)
#define PER1(i,a,b) PER(i,(a)+1,b)
#define PR1(i,n) PER1(i,n,1)
#define DO(n) RP(__i,n)
template<class T,class U>
bool cmax(T & a, const U & b) {return a < b ? a = b, 1 : 0;}
template<class T,class U>
bool cmin(T & a, const U & b) {return b < a ? a = b, 1 : 0;}

// data type
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define F first
#define S second

// STL container
typedef vector<int> vi;
typedef vector<ll> vll;
#define SZ(a) ((int)(a).size())
#define ALL(a) begin(a), end(a)
#define CLR(a) (a).clear()
#define BK(a) ((a).back())
#define FT(a) ((a).front())
#define DB(a) (a).pop_back()
#define DF(a) (a).pop_front()
#define PB push_back
#define EB emplace_back

/* I gave you my heart and then you turned around. */
void _BG(const char * s) {cerr<<s<<endl;};
template<class T, class ... TT>
void _BG(const char * s,T a, TT...b)
{
for (int c = 0; *s && (c || *s != ','); ++s) {
cerr<<*s;
for (char x : "([{") c += *s == x;
for (char x : ")]}") c -= *s == x;
}
cerr<<" = "<<a;
if (sizeof...(b)) {
cerr<<", ";
++s;
}
_BG(s,b...);
}
#define BG(...) do { \
cerr << __PRETTY_FUNCTION__ << ':' << __LINE__ << ": "; \
_BG(#__VA_ARGS__,__VA_ARGS__); \
} while(0)

/* Reading input is now 20% cooler! */
bool RD() {return 1;}
bool RD(char & a) {return scanf(" %c", &a) == 1;}
bool RD(char * a) {return scanf("%s", a) == 1;}
bool RD(double & a) {return scanf("%lf", &a) == 1;}
bool RD(int & a) {return scanf("%d", &a) == 1;}
bool RD(ll & a) {return scanf("%lld", &a) == 1;}

template<class T, class ... TT>
bool RD(T & a, TT & ... b) {return RD(a) && RD(b...);}

/* Do princesses dream of magic sheep? */
#define DR(T,...) T __VA_ARGS__; RD(__VA_ARGS__)
#define RI(...) DR(int,__VA_ARGS__)

/* For it's time for you to fulfill your output. */
void PT(const char & a) {putchar(a);}
void PT(char const * const & a) {fputs(a, stdout);}
void PT(const double & a) {printf("%.16f", a);}
void PT(const int & a) {printf("%d", a);}
void PT(const ll & a) {printf("%lld", a);}

/* The line will last forever! */
template<char s = ' ', char e = '\n'>
void PL() {if (e) PT(e);}
template<char s = ' ', char e = '\n', class T, class ... TT>
void PL(const T & a, const TT & ... b)
{PT(a); if (sizeof...(b) && s) PT(s); PL<s,e>(b...);}

/* Good Luck && Have Fun ! */
const int N = 2e5 + 87;
vi g[N], un;
void meld(vi & a, vi & b)
{
if (SZ(a) < SZ(b))
b.swap(a);
int i,j;
for (i = j = 0; i < SZ(b); ++i)
if (binary_search(ALL(a),b[i]))
b[j++] = b[i];
b.resize(j);
}
int main()
{
RI(n,m);
RP(i,m) {
RI(u,v);
--u,--v;
g[u].PB(v);
g[v].PB(u);
}
RP(i,n) {
sort(ALL(g[i]));
un.PB(i);
}
vi ans;
while (SZ(un)) {
vi no;
int x,k=0;
for(;;++k) {
bool ok = 0;
RP(i,SZ(un))
if (!binary_search(ALL(no),un[i])) {
ok = 1;
x = un[i];
un[i] = BK(un);
DB(un);
break;
}
if (!ok)
break;
if (k)
meld(g[x],no);
else
no.swap(g[x]);
}
ans.PB(k);
}
sort(ALL(ans));
PL(SZ(ans));
RP(i,SZ(ans))
PL<0,0>(ans[i]," \n"[i==SZ(ans)-1]);
}

0 comments on commit a221b4f

Please sign in to comment.