[Test room] 11.4

[Test room] 11.4

ssw02 cushions for a few days

Welcome to reprint ssw02's blog: https://www.cnblogs.com/ssw02/p/11794181.html


algebra

Fairy T1, interested persons can go to the next HEOI- Dodo blog.

decoration

Deco saying he should cut out the problem now.

O :

The first line of a positive integer n.
The next line n 1 or a positive integer, the i-th to fi + 1.
The next line number n, if the i-th 0 indicates Lin Xiansen wish i dot lights are turned off, if the i-th
number is represented by a dot i Lin Xiansen desirable lights are turned on.
Output line An integer representing a minimum of Linxian Sen takes a few seconds to see that he expected to see the tree.

Ideas :

Since N is small, it is considered to form pressure. Because any ideas at all, so consider DP.(Lwq: DP-shaped pressure regardless of family, regardless of what N ^ TM)

Taking into account one thing, Mr. Lin can activate a point (not MC) after, turn it off in the next second, the time will not exceed N. (You can output N + -rand () cheat points).

Change the we pretreated [i] [j] denotes the i-th point after activation effect on the entire tree j seconds. When taking into account the contribution of the sta | = change can be.

Due to continuous, if the order of enumeration, must eliminate the effects of a point one second earlier, before joining the impact, my approach would be one more ride complexity N.

But if the enumeration flashbacks, time varies. So disposed DP [i] [j] is a state j, i the second state may become a target. Such exclusive or only a change [i] [j-1] can.

Obtained state transition equation: dp [i + 1] [j ^ change [k] [i]] = dp [i] [j] true:? False; (k: 1 ~ (1 << N) -1)

Code

#include<bits/stdc++.h>
using namespace std ;
inline int read(){
    int s=0 ; char g=getchar() ; while(g>'9'||g<'0')g=getchar() ; 
    while( g>='0'&&g<='9' )s=s*10+g-'0',g=getchar() ; return  s ;
}
int N , sta , fa[20] , change[20][20] , dp[20][(1<<17)] ; 
void  prepare(){
    for( int i = 0 ; i <= N ; ++i )change[1][i] = 1 ;
    for( int i = 2 ; i <= N ; ++i ){
        int now = i ; 
        change[i][0] = ( 1<<(i-1) ) ; 
        for( int j = 1 ; j <= N ; ++j ){
            now = fa[now] ; 
            if(now) change[i][j] = (change[i][j-1]|(1<<(now-1))) ;
            else change[i][j] = change[i][j-1] ;//到顶了 
        }
    }
}
int main(){
    freopen("decoration.in","r",stdin) ; 
    freopen("decoration.out","w",stdout) ;
    N = read() ;  int m1 ; fa[1] = fa[0] = 0 ;
    for( int i = 1 ; i < N ; ++i )fa[i+1] = read() ; 
    for( int i = 1 ; i <= N ; ++i ){
        m1 = read() ;if(m1)sta |= (1<<(i-1) ) ;
    }
    prepare() ;
    dp[0][sta] = 1 ; // 状态为sta,0秒后状态为sta 
    for( int i = 0 ; i <=  N ; ++i ){
        if( dp[i][0] ){
            cout<<i ; return 0 ; 
        }
        for( int k = 1 ; k <= (1<<N)-1 ; ++k )
            if( dp[i][k] ){//状态为k,i秒后可以到达sta 
                dp[i+1][k] = true ; 
                for( int j = 1 ; j <= N ; ++j )dp[i+1][k^change[j][i]] = true ; 
            }
    }
    return 0;
}

lunch

To be honest, this question is quite cancer. . . . (Thinking Qing Qi)

We consider a conflict relationship between the various people:

If a man 0, then we can not control him in particular, to adapt to the situation. .

Considering a human-1, but he is a person and 1 dinner, then for the people must not learn to cancer 1 algorithm before dinner.

Note read the title: On the SPJ, so we can just let the program as legitimate.

If a person can learn to cancer algorithms i moment, then he will not be j (j> i) when the Society of cancer algorithms. The whole process as much as possible to push forward.

Specifically: Let sub h [] as a person first learned time cancer algorithms. Then he will be affected by a meal in people -1 influence. To make ahead of time as much as possible, eat when you select L, h [u] = L + 1.

Then consider the relationship between more people. So that (u, v) dinner. If [u]> R known f, then v and u eat before and certainly not learned, or no solution. This situation can lead h [u] - max (h [u], L + 1).

Considering the h [u] must meet all restrictions, it takes a maximum value. This thing can be the longest road maintenance, running once dijkstar.

Now maintain the h [] array, in considering the issue of a meal: dinner time everyone met: 1. legitimate (you can eat) 2 meet dis [i]> = h [i].. Let all the legal relationship between the above can be obtained (u learn algorithm cancer, taught to v, the shortest time is represented by dis array)

lim = max( h[to[i]] , max( L[i] , dis[u] ) ) 。 dis[to[i]] = min( dis[to[i]] , lim ) 。

Then, this is a dijkstra ......

Impossible to determine the ssw02 write the code.

Code:

#include<bits/stdc++.h>
using namespace std ;
const int MAXN = 200005 ;
inline int read(){
    int s=0,w=1 ; char g=getchar() ; while(g>'9'||g<'0'){if(g=='-')w=-1,g=getchar() ;} 
    while(g>='0'&&g<='9')s=s*10+g-'0',g=getchar() ; return s*w ;
}
int N , M , sta[MAXN] , h[MAXN] , L[MAXN*2] , R[MAXN*2] ;
int head[MAXN] , to[MAXN*2] , nex[MAXN*2] , tot = 1 ;
bool vis[MAXN] ; 
struct ap{
    int u , v , l , r ; 
}t[MAXN] ;
priority_queue<pair<int,int> >q ; 
void  add( int x , int y , int l , int r ){
    to[++tot] = y , nex[tot] = head[x] , L[tot] = l , R[tot] = r , head[x] = tot ; 
}
void  dijkstra(){//维护每个人学会的时间的下限 
    while( !q.empty() ){
        int  u = q.top().second ; q.pop() ; 
        if( vis[u] )continue ; 
        vis[u] = true ; 
        for( int i = head[u] ; i ; i = nex[i] ){//u,v在 L R 吃饭  , u 学会的时间 > R ,说明 u,v吃饭时v还没有学会,贪心在L吃饭,v在L+1学会 
            if( R[i] < h[u] )//出现限制情况 
                if( h[to[i]] < L[i]+1 ){//更新限制 
                    h[to[i]] = L[i]+1 ;
                    q.push( make_pair( h[to[i]],to[i] ) ) ; 
                }
        }
    }
}
int dis[MAXN] ; 
void  dijkstra2(){//维护每个人学会的时间 (贪心最早)
    while( !q.empty() )q.pop() ; 
    memset( vis , 0 , sizeof(vis) ) ;
    for( int i = 1 ; i <= N ; ++i )dis[i] = (1<<30) ; 
    q.push( make_pair(0,1) ) ; dis[1] = 0 ;
    while( !q.empty() ){
        int u = q.top().second ; q.pop() ;
        if( vis[u] )continue ; 
        vis[u] = true ; 
        for( int i = head[u] ; i ; i = nex[i] ){
            int  lim = max( h[to[i]] , max( L[i] , dis[u] ) ) ;//由 u 传授给 v : 满足条件 u会 , 能吃饭 
            if( lim <= R[i] )//合法 
                if( dis[ to[i] ] > lim ){
                    dis[ to[i] ] = lim ;
                    q.push( make_pair( -dis[to[i]] , to[i] ) ) ;
                }
        }
    }
}
void  print(){
    for( int i = 1 ; i <= M ; ++i ){
        if( sta[t[i].u] == -1 || sta[t[i].v] == -1 || ( t[i].r < max(dis[t[i].u] , dis[t[i].v]) ) )printf("%d\n",t[i].l ) ; 
        //有任意一个人不会,贪心最早吃饭 || 两个人吃饭时都不会 (任意时间吃)
        else printf("%d\n",max( max(dis[t[i].u],dis[t[i].v]) , t[i].l ) ) ;
    }
}
int main(){
    freopen("lunch.in","r",stdin) ;
    freopen("lunch.out","w",stdout) ; 
    N = read() , M = read() ; 
    for( int i = 1 ; i <= M ; ++i ){
        t[i].u = read() , t[i].v = read() , t[i].l = read() , t[i].r = read() ;
        add( t[i].u , t[i].v , t[i].l , t[i].r ) ; 
        add( t[i].v , t[i].u , t[i].l , t[i].r ) ;  
    }
    for( int i = 1 ; i <= N ; ++i ){
        sta[i] = read() ; 
        if( sta[i] == -1 ){
            h[i] = (1<<30) ; q.push( make_pair( h[i] , i ) ) ;
        }
    }
    dijkstra() ; dijkstra2() ; bool flag = 1 ; 
    for( int i = 1 ; i <= N ; ++i )//最后会但限制不会,冲突 
        if( sta[i] == 1 && dis[i] == (1<<30) )flag = 0 ; 
    for( int i = 1 ; i <= M ; ++i ){
        if( sta[ t[i].u ]==-1 && dis[ t[i].v ] <= t[i].l )flag = 0 ;//明明不会有还在和会的吃饭 
        if( sta[ t[i].v ]==-1 && dis[ t[i].u ] <= t[i].l )flag = 0 ;
    }
    if( flag )print() ; 
    else cout<<"Impossible" ;
    return 0 ;
}

to sum up

T1, zhen ^ TM cancer (I almost wrote Huarong enhanced version ....)

T2 is the state transition equation of classic design, when the unknown target, taking into account the change state of the array is fixed to the target time, for convenience, it is the state transition flashback design.

T3 belongs very good idea band limitation problem into the shortest path problem. A plurality of band limitation condition is satisfied, (the difference and thought quite similar constraints).

Sentenced questions still have problems today exams in T1 obvious panic when better practice, affecting the attitude of the entire exam. T2 and T3 is thinking + routine questions, and sometimes did not lead to a breakthrough ssw02 miserable death. . . (Examination went back to review the PPT ...) on T1 really spend too much time, and are each packed data card (subtask kill !!!), tomorrow, be sure to verify the algorithm, do not dependence done problem. . . .

Guess you like

Origin www.cnblogs.com/ssw02/p/11794181.html