Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- */
- //#pragma comment(linker, "/STACK:16777216")
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <complex>
- #include <math.h>
- #include <set>
- #include <vector>
- #include <map>
- #include <queue>
- #include <stdio.h>
- #include <stack>
- #include <algorithm>
- #include <list>
- #include <ctime>
- #include <memory.h>
- #include <ctime>
- #include <assert.h>
- #define y0 sdkfaslhagaklsldk
- #define y1 aasdfasdfasdf
- #define yn askfhwqriuperikldjk
- #define j1 assdgsdgasghsf
- #define tm sdfjahlfasfh
- #define lr asgasgash
- #define eps 1e-8
- #define M_PI 3.141592653589793
- #define bs 1000000007
- #define bsize 512
- using namespace std;
- int n,prod[1<<20];
- vector<int> emp[1<<20];
- int m;
- vector<int> g[1<<20];
- int bound[1<<20];
- int val[1<<20];
- long long ans;
- int set_emp(int id)
- {
- int res=bound[id];
- for (int i=0;i<g[id].size();i++)
- {
- int to=g[id][i];
- res=max(res,bound[to]);
- }
- return res;
- }
- void update1(int id)
- {
- bound[id]=max(bound[id],val[id]);
- for(int i=0;i<g[id].size();i++)
- {
- int to=g[id][i];
- bound[to]=max(bound[to],val[id]);
- }
- }
- void update_emp(int id)
- {
- bound[id]=max(bound[id],val[id]);
- for(int i=0;i<g[id].size();i++)
- {
- int to=g[id][i];
- bound[to]=max(bound[to],val[id]+1);
- }
- }
- int main(){
- //freopen("beavers.in","r",stdin);
- //freopen("beavers.out","w",stdout);
- //freopen("F:/in.txt","r",stdin);
- //freopen("F:/output.txt","w",stdout);
- ios_base::sync_with_stdio(0);
- //cin.tie(0);
- cin>>n;
- for (int i=1;i<=n;i++)
- {
- cin>>prod[i];
- assert(prod[i]>=1&&prod[i]<=100000);
- emp[prod[i]].push_back(i);
- }
- cin>>m;
- for (int i=1;i<=m;i++)
- {
- int a,b;
- cin>>a>>b;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- for (int i=1;i<=n;i++)
- bound[i]=1;
- for (int i=1;i<=100000;i++)
- random_shuffle(emp[i].begin(),emp[i].end());
- for (int i=1;i<=100000;i++)
- {
- for (int j=0;j<emp[i].size();j++)
- {
- int id=emp[i][j];
- int cost=set_emp(id);
- ans+=cost;
- val[id]=cost;
- update1(id);
- }
- while (true)
- {
- int changes=0;
- for (int j=0;j<emp[i].size();j++)
- {
- int id=emp[i][j];
- int cost=set_emp(id);
- if (val[id]<cost)
- {
- ans+=cost-val[id];
- val[id]=cost;
- changes=1;
- update1(id);
- }
- }
- if (changes==0)
- break;
- }
- for (int j=0;j<emp[i].size();j++)
- {
- int id=emp[i][j];
- update_emp(id);
- }
- }
- cout<<ans<<endl;
- //cin.get();cin.get();
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement