随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

 

多判断一个唯一性

 only[ x] [0/1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int N=205;
int f[N][2],n,K;
int only[N][3] ;
 vector<int> g[N];
 map<string,int> mp;
  
 void dfs(int x){
    f[x][1]=1; f[x][0]=0;
    for(auto y:g[x]){
        dfs(y);
         
        f[x][0]+=max(f[y][0],f[y][1]);
        f[x][1]+=f[y][0];
    }
    for(auto v:g[x]){
         if(f[v][1]>f[v][0]&&only[v][1]) 
            only[x][0]=1;
        else if(f[v][0]==f[v][1])           
            only[x][0]=1;
        else if(f[v][1]<f[v][0]&&only[v][0])
            only[x][0]=only[x][1]=1;
    }
 }
 int len;
 void sov(){
    len=0; mp.clear();
    for(int i=0;i<N;i++) g[i].clear() ;
    memset(only,0,sizeof only );
    memset(f,0,sizeof f) ;
    string s1,s2;
     
    cin>>s1;
    mp[s1]=++len;
    for(int i=1;i<n;i++){
        cin>>s1>>s2;
         
        if(mp.count(s1)==0) mp[s1]=++len;
        if(mp.count(s2)==0) mp[s2]=++len;
         
    //  cout<<"add " <<mp[s2]<<' '<<mp[s1]<<endl;
        g[mp[s2]].push_back(mp[s1]);
    }
    dfs(1) ;
        if(f[1][0]<f[1][1]){
            printf("%d ",f[1][1]);
            puts(only[1][1]?"No":"Yes");
        }
        else if(f[1][0]>f[1][1]){
            printf("%d ",f[1][0]);
            puts(only[1][0]?"No":"Yes");
        }
        else {
            printf("%d ",f[1][0]);
            puts("No");
        }
 }
 int main(){
    while(cin>>n&&n) sov() ;
 }

 

posted on   towboat  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示