【日常小测】Different Trips

题目大意

给定一棵$n(n \le 10^5)$个结点的树,每一个结点的特征值定义为这个结点的度
询问从根节点走到叶子结点的所有路径中本质不同的字符串的数量

解题报告

这题就是诸神眷顾的幻想乡的弱化版,撸一发广义SAM就可以了

不过GEOTCBRL考完之后给我们增加了一点姿势水平:

广义后缀自动机如果使用DFS建立的话,时间复杂度是$O($叶子结点深度和$)$的
如果使用BFS来建立的话,时间复杂度是$O(n \cdot$字符集大小$)$

这在15年刘研绎的集训队论文《后缀自动机在字典树上的拓展》中有详细的证明
其中还给出了构造极端数据的构造方法
所以这题字符集大小这么大的话,这样会T的
可能只有后缀数组才能做 QwQ

Code

这辣鸡代码是可以卡的,泥萌不要卡啊…….

#include<bits/stdc++.h>
#define LL long long
using namespace std;
 
const int N = 2000009;
 
int n,head[N],nxt[N],to[N],in[N];
 
inline int read() {
    char c=getchar(); int ret=0,f=1;
    while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
    return ret*f;
}
 
inline void AddEdge(int u, int v) {
    static int E = 1; in[u]++; in[v]++;
    to[++E] = v; nxt[E] = head[u]; head[u] = E;
    to[++E] = u; nxt[E] = head[v]; head[v] = E;
}
 
class Suffix_Automaton{
    int tot,len[N],fail[N],vis;
    map<int,int> ch[N];
    public:
        inline Suffix_Automaton() {tot=1;}
        inline int insert(int t, int c) {
            if (ch[t]&&len[ch[t]]==len[t]+1) return ch[t];
            int w = ++tot; len[w] = len[t] + 1;
            for (;t&&!ch[t];t=fail[t]) ch[t] = w;
            if (!t) fail[w] = 1;
            else if (len[ch[t]] == len[t] + 1) fail[w] = ch[t];
            else {
                int nt = ++tot, pt = ch[t];
                ch[nt] = ch[pt]; len[nt] = len[t] + 1;
                fail[nt] = fail[pt]; fail[pt] = fail[w] = nt;
                for (;t&&ch[t]==pt;t=fail[t]) ch[t] = nt;
            } return w;
        }
        inline LL GetAns() {
            LL ret = 0;
            for (int i=2;i<=tot;i++)
                ret += len[i] - len[fail[i]];
            return ret;
        }
}SAM;
 
void DFS(int w, int f, int ins) {
    int nins = SAM.insert(ins, in[w]);
    for (int i=head[w];i;i=nxt[i]) {
        if (to[i] != f) {
            DFS(to[i], w, nins);
        }
    }
}
 
int main() {
    n = read();
    for (int i=1;i<n;i++) AddEdge(read(), read());
    DFS(1, 1, 1);
    printf("%lld\n",SAM.GetAns());
    return 0;
}

87 thoughts to “【日常小测】Different Trips”

  1. you are in point of fact a excellent webmaster. The site
    loading velocity is amazing. It sort of feels that you are doing any unique trick.
    Furthermore, The contents are masterpiece. you’ve done a wonderful
    task in this matter!

  2. First of all I want to say superb blog! I had a quick question that I’d like to ask
    if you don’t mind. I was curious to know how you
    center yourself and clear your mind before writing.

    I have had difficulty clearing my thoughts in getting
    my ideas out there. I truly do enjoy writing but it just seems
    like the first 10 to 15 minutes are lost simply just trying to figure out how to begin. Any suggestions
    or hints? Kudos!

  3. First off I want to say excellent blog! I had a quick question in which I’d like to ask if you
    do not mind. I was interested to know how
    you center yourself and clear your head before writing.
    I’ve had a tough time clearing my mind in getting
    my ideas out there. I do enjoy writing however it just seems like the first 10 to
    15 minutes are lost just trying to figure
    out how to begin. Any ideas or hints? Cheers!

  4. Excellent weblog here! Additionally your website lots up very fast!
    What host are you the use of? Can I get your affiliate link on your host?
    I wish my web site loaded up as fast as yours lol

  5. I have been exploring for a little for any high quality articles or blog posts on this
    kind of house . Exploring in Yahoo I finally stumbled upon this
    site. Reading this information So i am happy to express that
    I’ve an incredibly just right uncanny feeling I found out just what I needed.
    I most no doubt will make certain to don?t fail to remember this site and give
    it a look regularly.

  6. I love your blog.. very nice colors & theme. Did you create this website yourself or did you
    hire someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to know where u got
    this from. thank you

  7. Hey there just wanted to give you a quick heads up
    and let you know a few of the pictures aren’t loading
    correctly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both show the same outcome.

    natalielise pof

  8. I think that is one of the most vital information for me.
    And i am satisfied studying your article. However wanna observation on few
    basic things, The website style is perfect, the articles is actually excellent :
    D. Just right activity, cheers

  9. Thanks for any other great post. The place else may just anybody
    get that kind of info in such a perfect method of writing?

    I’ve a presentation next week, and I am at the look for such info.

  10. Pretty section of content. I just stumbled upon your web site and in accession capital to assert that I acquire
    actually enjoyed account your blog posts. Any way I
    will be subscribing to your augment and even I achievement you access consistently rapidly.

  11. Hello there! I know this is kind of off topic
    but I was wondering if you knew where I could get a captcha
    plugin for my comment form? I’m using the same blog platform
    as yours and I’m having trouble finding one?
    Thanks a lot!

  12. Hello, i think that i saw you visited my web
    site thus i came to “return the favor”.I’m attempting
    to find things to enhance my web site!I suppose its ok to use some of your
    ideas!!

  13. Howdy! Someone in my Facebook group shared this site with us so I came to take a look.
    I’m definitely enjoying the information. I’m book-marking and will
    be tweeting this to my followers! Wonderful blog and outstanding design.

  14. Hey There. I discovered your blog using msn. That is a really smartly written article.

    I’ll be sure to bookmark it and come back to learn more
    of your useful info. Thank you for the post. I’ll certainly comeback.

  15. Hi! I know this is somewhat off topic but I was wondering which
    blog platform are you using for this website? I’m getting sick and
    tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform.
    I would be fantastic if you could point
    me in the direction of a good platform.

  16. Wow, incredible weblog layout! How lengthy have you been blogging
    for? you make blogging glance easy. The entire look of your site is magnificent,
    let alone the content!

  17. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your webpage?
    My blog is in the exact same area of interest as yours and my visitors would certainly benefit from a lot of the information you present here.
    Please let me know if this okay with you. Regards!

  18. Excellent beat ! I wish to apprentice while you amend
    your site, how can i subscribe for a blog web site? The account helped me
    a acceptable deal. I had been a little bit acquainted of
    this your broadcast provided bright clear idea

  19. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment.
    Is there any way you can remove people from that service?

    Thanks a lot!

  20. Thanks for ones marvelous posting! I truly enjoyed reading it, you might be a great author.I will make
    certain to bookmark your blog and will come back later on. I want to encourage you continue your great posts, have a nice day!

  21. Greetings I am so delighted I found your webpage, I really found you by mistake, while I
    was browsing on Bing for something else, Nonetheless I am here now and would
    just like to say many thanks for a fantastic post and a all round thrilling blog
    (I also love the theme/design), I don’t have time to read it all at
    the moment but I have book-marked it and also added your RSS feeds,
    so when I have time I will be back to read a lot more, Please
    do keep up the superb work.

  22. It is the best time to make some plans for the
    future and it is time to be happy. I’ve read this
    post and if I could I desire to suggest you few interesting things or advice.
    Perhaps you could write next articles referring to this article.
    I wish to read even more things about it!

  23. I’m amazed, I have to admit. Seldom do I encounter a blog that’s both equally educative and interesting, and let me tell
    you, you’ve hit the nail on the head. The problem is something not enough
    people are speaking intelligently about. I’m very happy that
    I found this during my search for something relating
    to this.

  24. Hey I am so excited I found your web site, I really found
    you by error, while I was researching on Yahoo for something else, Regardless I am here
    now and would just like to say thanks for a incredible post and a all round enjoyable
    blog (I also love the theme/design), I don’t have time to
    look over it all at the moment but I have bookmarked it and also included your RSS
    feeds, so when I have time I will be back to
    read more, Please do keep up the fantastic work.

  25. Thanks for one’s marvelous posting! I really enjoyed reading it, you are a great author.I will make sure to bookmark your blog and will come back in the foreseeable future.
    I want to encourage you to ultimately continue your great job, have a nice holiday weekend!

  26. Hello, Neat post. There is a problem together
    with your site in internet explorer, would check this?
    IE still is the marketplace chief and a large component to people will leave
    out your fantastic writing because of this problem.

  27. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same
    comment. Is there any way you can remove me from that service?
    Thank you!

  28. My partner and I stumbled over here coming from a different web
    page and thought I might check things out. I like what I see so now i am following you.
    Look forward to checking out your web page again.

  29. Wonderful goods from you, man. I have be aware your stuff prior to and you’re simply extremely excellent.
    I actually like what you’ve received right here,
    really like what you are stating and the best way by which you say it.
    You are making it entertaining and you continue to take care
    of to keep it sensible. I can’t wait to read much more from you.
    This is actually a wonderful website.

  30. Hello there, just became aware of your blog through Google, and found that it is really informative.
    I am going to watch out for brussels. I will be grateful
    if you continue this in future. Lots of people will be benefited from your writing.

    Cheers!

  31. Thank you for every other fantastic post. The place else could anybody get
    that type of info in such an ideal way of writing? I’ve a presentation next
    week, and I’m on the look for such info.

  32. Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation;
    we have created some nice practices and we are looking
    to swap solutions with others, why not shoot me an e-mail if interested.

  33. Pretty section of content. I just stumbled upon your site and in accession capital to say that I acquire in fact loved account your weblog posts.
    Any way I will be subscribing for your augment and even I fulfillment you get right of entry to persistently quickly.

  34. I don’t even know how I ended up here, but I thought this post was great.
    I do not know who you are but definitely you’re going to a famous blogger if
    you aren’t already 😉 Cheers!

  35. Hello, I think your website might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it has
    some overlapping. I just wanted to give you a quick heads up!
    Other then that, excellent blog!

  36. We are a gaggle of volunteers and opening a new
    scheme in our community. Your web site provided us with useful
    info to work on. You’ve done a formidable job and our entire neighborhood might be thankful to you.

  37. Hi, I do think this is an excellent web site. I stumbledupon it 😉 I am going to come
    back yet again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

  38. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You obviously know what youre talking about, why waste your intelligence on just
    posting videos to your site when you could be giving us something enlightening to read?

  39. Hi there, I found your web site by way of Google while looking for a similar matter, your site came up, it appears
    good. I have bookmarked it in my google bookmarks.

    Hi there, simply become alert to your weblog thru Google, and found that it’s truly informative.
    I am gonna watch out for brussels. I will appreciate in case you continue this in future.
    Many other people will probably be benefited from your writing.
    Cheers!

  40. Magnificent goods from you, man. I’ve remember your stuff previous to and
    you are just too magnificent. I actually like what you have
    bought here, certainly like what you’re stating and the way through which you say it.
    You’re making it entertaining and you still take care of to
    stay it wise. I cant wait to learn much more from you.
    This is actually a great site.

  41. Today, while I was at work, my cousin stole my iPad and tested to see if it can survive
    a 30 foot drop, just so she can be a youtube sensation. My iPad is now broken and
    she has 83 views. I know this is entirely off topic but I had to share it with someone!

  42. This design is incredible! You most certainly know how to
    keep a reader amused. Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Fantastic job. I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

  43. An outstanding share! I’ve just forwarded this
    onto a colleague who was doing a little research on this.
    And he actually ordered me breakfast simply because I stumbled upon it for him…

    lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending the time to talk about this subject here on your blog.

  44. Simply want to say your article is as surprising. The clarity in your post is simply great and i could assume you’re an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the gratifying work.

  45. I think this is among the most vital information for
    me. And i am glad reading your article. But should remark on few general things, The web site
    style is great, the articles is really great : D.
    Good job, cheers

  46. Your style is so unique compared to other people I
    have read stuff from. Thanks for posting when you’ve got the opportunity, Guess I will just bookmark this page.

  47. I like the valuable information you supply on your articles.
    I will bookmark your weblog and test again right here regularly.
    I’m slightly certain I will be informed many new stuff right
    right here! Good luck for the following!

  48. Hi! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up losing
    months of hard work due to no back up. Do you have any solutions to protect against hackers?

  49. Thanks a bunch for sharing this with all of us you
    really recognize what you’re speaking about!

    Bookmarked. Please also consult with my web site =). We could have a link change arrangement among us

  50. My family every time say that I am killing my time here
    at web, but I know I am getting familiarity all the time by reading such nice content.

  51. Hello! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really appreciate
    your content. Please let me know. Cheers

  52. Hi there would you mind sharing which blog platform
    you’re working with? I’m looking to start my own blog in the near future but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs
    and I’m looking for something unique. P.S My apologies for getting off-topic but I had to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *