<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Codeforces Round #603 (Div. 2) F. Economic Difficulties dp

      F. Economic Difficulties

      An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!

      Each grid (main and reserve) has a head node (its number is 1). Every other node gets electricity from the head node. Each node can be reached from the head node by a unique path. Also, both grids have exactly n nodes, which do not spread electricity further.

      In other words, every grid is a rooted directed tree on n leaves with a root in the node, which number is 1. Each tree has independent enumeration and nodes from one grid are not connected with nodes of another grid.

      Also, the palace has n electrical devices. Each device is connected with one node of the main grid and with one node of the reserve grid. Devices connect only with nodes, from which electricity is not spread further (these nodes are the tree's leaves). Each grid's leaf is connected with exactly one device.

      In this example the main grid contains 6 nodes (the top tree) and the reserve grid contains 4 nodes (the lower tree). There are 3 devices with numbers colored in blue.
      It is guaranteed that the whole grid (two grids and n devices) can be shown in this way (like in the picture above):

      main grid is a top tree, whose wires are directed 'from the top to the down',
      reserve grid is a lower tree, whose wires are directed 'from the down to the top',
      devices — horizontal row between two grids, which are numbered from 1 to n from the left to the right,
      wires between nodes do not intersect.
      Formally, for each tree exists a depth-first search from the node with number 1, that visits leaves in order of connection to devices 1,2,…,n (firstly, the node, that is connected to the device 1, then the node, that is connected to the device 2, etc.).

      Businessman wants to sell (remove) maximal amount of wires so that each device will be powered from at least one grid (main or reserve). In other words, for each device should exist at least one path to the head node (in the main grid or the reserve grid), which contains only nodes from one grid.

      Input

      The first line contains an integer n (1≤n≤1000) — the number of devices in the palace.

      The next line contains an integer a (1+n≤a≤1000+n) — the amount of nodes in the main grid.

      Next line contains a?1 integers pi (1≤pi≤a). Each integer pi means that the main grid contains a wire from pi-th node to (i+1)-th.

      The next line contains n integers xi (1≤xi≤a) — the number of a node in the main grid that is connected to the i-th device.

      The next line contains an integer b (1+n≤b≤1000+n) — the amount of nodes in the reserve grid.

      Next line contains b?1 integers qi (1≤qi≤b). Each integer qi means that the reserve grid contains a wire from qi-th node to (i+1)-th.

      The next line contains n integers yi (1≤yi≤b) — the number of a node in the reserve grid that is connected to the i-th device.

      It is guaranteed that each grid is a tree, which has exactly n leaves and each leaf is connected with one device. Also, it is guaranteed, that for each tree exists a depth-first search from the node 1, that visits leaves in order of connection to devices.

      Output

      Print a single integer — the maximal amount of wires that can be cut so that each device is powered.

      Examples

      input
      3
      6
      4 1 1 4 2
      6 5 3
      4
      1 1 1
      3 4 2
      output
      5
      input
      4
      6
      4 4 1 1 1
      3 2 6 5
      6
      6 6 1 1 1
      5 4 3 2
      output
      6
      input
      5
      14
      1 1 11 2 14 14 13 7 12 2 5 6 1
      9 8 3 10 4
      16
      1 1 9 9 2 5 10 1 14 3 7 11 6 12 2
      8 16 13 4 15
      output
      17

      Note

      For the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red):

      The second and the third examples can be seen below:

      題意

      給你兩棵樹,每棵樹都恰好有n個葉子,每個葉子都連著一個電機(jī)。

      讓你刪除最多的邊,使得每個電機(jī)都至少能夠存在一條路徑到某棵樹的根。

      note很清楚

      題解

      視頻題解 https://www.bilibili.com/video/av77514280/

      對于每棵樹,維護(hù)l[i][j]表示我刪掉這個子樹的所有邊之后,[i,j]這個范圍的電機(jī)不保證能夠全部連上我的根。

      用一個dp[i]表示[1,i]區(qū)間內(nèi),全都能連上根最多能刪除多少條邊,那么轉(zhuǎn)移就是dp[i]=max(dp[i],dp[j-1]+max(l[j][i]));這樣轉(zhuǎn)移。

      代碼

      #include<bits/stdc++.h>
      using namespace std;
      const int maxn = 2005;
      vector<int>G[2][maxn];
      int dp[maxn],val[2][maxn][maxn],l[2][maxn],r[2][maxn],sz[2][maxn];
      int n,a;
      void dfs(int _,int x){
      	if(x!=1)sz[_][x]=1;
      	for(int i=0;i<G[_][x].size();i++){
      		int v = G[_][x][i];
      		dfs(_,v);
      		l[_][x]=min(l[_][x],l[_][v]);
      		r[_][x]=max(r[_][x],r[_][v]);
      		sz[_][x]+=sz[_][v];
      	}
      	val[_][l[_][x]][r[_][x]]=max(val[_][l[_][x]][r[_][x]],sz[_][x]);
      }
      int main(){
      	cin>>n;
      	for(int _=0;_<2;_++){
      		cin>>a;
      		for(int i=1;i<=a;i++){
      			l[_][i]=a+1;
      			r[_][i]=0;
      		}
      		for(int i=2;i<=a;i++){
      			int x;cin>>x;
      			G[_][x].push_back(i);
      		}
      		for(int i=1;i<=n;i++){
      			int x;cin>>x;
      			l[_][x]=r[_][x]=i;
      		}
      		dfs(_,1);
      	}
      	for(int i=1;i<=n;i++){
      		for(int j=i;j<=n;j++){
      			dp[j]=max(dp[j],dp[i-1]+max(val[0][i][j],val[1][i][j]));
      		}
      	}
      	cout<<dp[n]<<endl;
      }
      
      posted @ 2019-11-30 11:43  qscqesze  閱讀(640)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 九九热在线免费精品视频| 婷婷六月天在线| 午夜福利啪啪片| 成人精品区| 三上悠亚精品二区在线观看| 日韩亚洲国产中文字幕欧美| 久久久久中文伊人久久久| 人妻少妇精品视频专区| 国产97人人超碰caoprom| 亚洲精品一区二区三区大桥未久| 国自产拍偷拍精品啪啪模特| 亚洲欧美中文字幕日韩一区二区| 久久中文字幕国产精品| 摸丰满大乳奶水www免费| 一区二区三区四区高清自拍| 高清一区二区三区不卡视频| 色综合视频一区二区三区| 国产精品疯狂输出jk草莓视频| 一个色综合国产色综合| 国产精品色内内在线播放| 日本精品一区二区不卡| 亚欧洲乱码视频一二三区| 亚洲国产成人久久精品APP| 国产成人一区二区不卡| 看全色黄大黄大色免费久久| 人与禽交av在线播放| 久久精品国产久精国产| 亚洲日本va午夜中文字幕久久 | 中文字幕久久久久人妻| 国产av一区二区不卡| 性男女做视频观看网站| 国产内射性高湖| 精品91在线| 巨爆乳中文字幕爆乳区| 少妇人妻av无码专区| 国产成人8X人网站视频| 乱人伦中文字幕成人网站在线| 男同精品视频免费观看网站| 精品熟女日韩中文十区| 又大又紧又粉嫩18p少妇| 石原莉奈日韩一区二区三区|