【Codeforces 715C】Digit Tree

相关链接

题目传送门:http://codeforces.com/problemset/problem/715/C
官方题解:http://codeforces.com/blog/entry/47169

解题报告

要求统计树上路径,那么基本上确定是DP或者树分治了
想一想,好像DP的状态不好表示的样子,于是就直接点分治啦!

考虑对于每一个中心,统计经过该点符合要求的路径数
很明显需要将路径剖成两半,一半扔到map里,另一半直接查

但这题还有需要注意的就是如何去掉两段都在同一子树的非法情况
似乎直接像之前一样在子树的根部调用cal()直接剪掉的方法不管用了
于是可以先DFS一遍,统计所有信息
然后再处理每一个子树的时候,先DFS一遍,把该子树的信息给去掉
查询完成之后,再DFS一遍把信息给加回去

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 200000 + 9;

int head[N],nxt[N],cost[N],to[N],REV[N];
int n,MOD; LL vout;

inline int read() {
	char c=getchar(); int f=1,ret=0;
	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 Add_Edge(int u, int v, int w) {
	static int T = 0;
	to[++T] = v; nxt[T] = head[u]; head[u] = T; cost[T] = w;
	to[++T] = u; nxt[T] = head[v]; head[v] = T; cost[T] = w; 
}

void gcd(int a, LL &x, int b, LL &y) {
	if (!b) {x = 1, y = 0;}
	else {gcd(b,y,a%b,x);y-=a/b*x;}
}

inline int gcd(int a, int b) {
	static LL x,y;
	gcd(a,x,b,y);
	return (x % MOD + MOD) % MOD;
} 

namespace Node_Decomposition{
	#define ND Node_Decomposition
	const int INF = 1e9;
	int tot,node_sz,root,cur;
	int sum[N],dep[N],vis[N];
	map<int,int> cnt;
	
	void Get_Root(int w, int f) {
		sum[w] = 1; int mx = 0;
		for (int i=head[w];i;i=nxt[i]) {
			if (to[i] != f && !vis[to[i]]) {
				Get_Root(to[i], w);
				sum[w] += sum[to[i]];
				mx = max(mx, sum[to[i]]);
			}
		}
		mx = max(mx, node_sz - sum[w]);
		if (mx < cur) cur = mx, root = w;
	}
	
	void DFS(int w, int f, int delta, LL p, int val) {
		cnt[val] += delta; 
		for (int i=head[w];i;i=nxt[i]) {
			if (!vis[to[i]] && to[i] != f) {
				DFS(to[i], w, delta, p * 10 % MOD, (val + cost[i] * p) % MOD);
			}
		}
	}
	
	void cal(int w, int f, int t, LL val) {
		vout += cnt[(-val*REV[t]%MOD+MOD)%MOD]; 
		for (int i=head[w];i;i=nxt[i]) {
			if (!vis[to[i]] && to[i] != f) {
				cal(to[i], w, t+1, (val * 10 + cost[i]) % MOD);
			}
		}
	}
	
	void solve(int w, int sz) {
		vis[w] = 1; cnt.clear(); 
		for (int i=head[w];i;i=nxt[i]) {
			if (!vis[to[i]]) {
				DFS(to[i], w, 1, 10 % MOD, cost[i] % MOD);
			}
		}
		vout += cnt[0]; cnt[0]++;
		for (int i=head[w];i;i=nxt[i]) {
			if (!vis[to[i]]) {
				DFS(to[i], w, -1, 10 % MOD, cost[i] % MOD);
				cal(to[i], w, 1, cost[i] % MOD);
				DFS(to[i], w, 1, 10 % MOD, cost[i] % MOD);
			}
		}
		for (int i=head[w];i;i=nxt[i]) {
			if (!vis[to[i]]) {
				node_sz = sum[to[i]] > sum[w] ? sz - sum[w] : sum[to[i]];
				cur = INF; Get_Root(to[i], w);
				solve(root, node_sz);
			}
		}
	}
	
	inline void solve() {
		cur = INF; node_sz = n;
		Get_Root(1,1);
		solve(root,n);
	}
};

int main() {
	n = read(); MOD = read();
	for (int i=1,u,v,w;i<n;i++) {
		u = read(); v = read(); w = read();
		Add_Edge(u + 1, v + 1, w);
	}
	REV[0] = 1; REV[1] = gcd(10, MOD);
	for (int i=2;i<=n;i++)
		REV[i] = (LL)REV[i-1] * REV[1] % MOD;
	ND::solve();
	printf("%lld\n",vout);
	return 0;
}

172 thoughts to “【Codeforces 715C】Digit Tree”

  1. Attractive section of content. I just stumbled upon your weblog and in accession capital
    to assert that I acquire in fact enjoyed account your blog
    posts. Any way I will be subscribing to your augment
    and even I achievement you access consistently fast.

  2. Hi there! Quick question that’s totally off topic. Do you know how to
    make your site mobile friendly? My blog looks weird when browsing from my
    iphone4. I’m trying to find a theme or plugin that might be able to correct this problem.

    If you have any recommendations, please share. Thanks!

  3. Hello there, I found your blog by way of Google while
    searching for a comparable matter, your web site came up, it seems to
    be good. I have bookmarked it in my google bookmarks.

    Hello there, just changed into aware of your blog
    via Google, and located that it is truly informative. I
    am going to watch out for brussels. I’ll appreciate if
    you happen to proceed this in future. Numerous other folks shall be benefited out of
    your writing. Cheers!

  4. Hello there, I discovered your site by way of Google even as looking for a related matter, your web site got here up,
    it appears great. I have bookmarked it in my google bookmarks.

    Hi there, simply changed into alert to your weblog via Google,
    and found that it’s really informative. I am going to be careful for brussels.

    I’ll appreciate should you continue this in future.

    A lot of other folks might be benefited from your
    writing. Cheers!

  5. you’re in reality a just right webmaster. The web site loading velocity is incredible.

    It seems that you are doing any unique trick. In addition, The contents are masterpiece.
    you’ve done a excellent task in this matter!

  6. Sweet blog! I found it while browsing on Yahoo News.

    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thank you

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

  8. Hello! This is my first visit to your blog! We are a team of volunteers
    and starting a new initiative in a community in the same niche.
    Your blog provided us valuable information to work
    on. You have done a extraordinary job!

  9. Do you mind if I quote a few of your posts as long as I
    provide credit and sources back to your site?
    My blog is in the very same area of interest as yours
    and my users would genuinely benefit from some
    of the information you present here. Please let me know if this okay with you.
    Thanks!

  10. Hi there I am so thrilled I found your website, I really found
    you by error, while I was searching on Bing for something else,
    Nonetheless I am here now and would just like to say thanks for a marvelous 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 book-marked it and also added your RSS feeds, so when I have time I will be back to read much more, Please do keep up
    the excellent jo.

  11. Unquestionably believe that which you said. Your favorite justification seemed to
    be on the web the easiest thing to be aware of.
    I say to you, I certainly get irked while people think about worries that they
    plainly do not know about. You managed to hit the nail upon the top and
    also defined out the whole thing without having side-effects , people
    could take a signal. Will probably be back to get more.
    Thanks

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

  13. I do accept as true with all of the ideas you have introduced for your post.
    They are very convincing and can definitely work.

    Still, the posts are too short for beginners. May you
    please extend them a bit from next time? Thanks for the post.

  14. What’s up mates, how is all, and what you desire to say on the topic
    of this article, in my view its truly amazing designed for me.
    plenty of fish natalielise

  15. obviously like your web site but you need to take a look at
    the spelling on quite a few of your posts. Many of them are rife with spelling problems and I find
    it very troublesome to inform the reality on the other hand I will certainly come back
    again.

  16. Fantastic goods from you, man. I’ve understand your stuff previous
    to and you’re just too excellent. I actually like what you have
    acquired here, really like what you’re stating and the way in which you say
    it. You make it entertaining and you still care for to keep it wise.
    I can’t wait to read far more from you. This is
    really a tremendous web site.

  17. Wonderful blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Cheers

  18. You actually make it appear really easy along with your presentation but I in finding this matter to be really
    one thing that I feel I might by no means understand.
    It sort of feels too complicated and very huge for me.
    I’m having a look ahead in your next publish, I will try to get
    the hang of it!

  19. Howdy! I could have sworn I’ve been to this web site before but after browsing through some of the posts I realized it’s new to me. Anyways, I’m certainly happy I found it and I’ll be bookmarking it and checking back frequently!

  20. Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is fundamental and all.
    However think about if you added some great pictures or videos to give your posts more, “pop”!
    Your content is excellent but with pics and videos, this website could undeniably be one of the greatest in its niche.

    Good blog!

  21. Howdy! Someone in my Facebook group shared this site
    with us so I came to look it over. I’m definitely enjoying the information.
    I’m book-marking and will be tweeting this to my followers!
    Terrific blog and excellent design and style.

  22. Greetings I am so grateful I found your web site, I really found
    you by accident, while I was looking on Digg for something else, Anyways
    I am here now and would just like to say thanks for a tremendous post and a all round exciting blog (I
    also love the theme/design), I don’t have time to read
    through it all at the moment but I have bookmarked it and
    also added your RSS feeds, so when I have time I will
    be back to read much more, Please do keep up the awesome jo.

  23. After looking into a few of the blog posts on your website,
    I truly like your way of writing a blog. I book-marked it to my bookmark
    site list and will be checking back in the near future.
    Please check out my web site as well and tell me how
    you feel.

  24. Incredible! This blog looks exactly like my old one! It’s on a totally different subject
    but it has pretty much the same layout and design. Great choice of colors!

  25. I’m pretty pleased to uncover this great site. I wanted to thank you for your time due to this fantastic read!!

    I definitely savored every little bit of it and i also have you saved to fav to
    look at new things in your blog.

  26. Hey there! This is my first comment here so I
    just wanted to give a quick shout out and say
    I truly enjoy reading through your posts. Can you recommend any other blogs/websites/forums that deal with the same subjects?
    Appreciate it!

  27. Hey There. I found your blog using msn. This is a very well written article.
    I’ll be sure to bookmark it and return to read more of your useful information. Thanks for the
    post. I’ll certainly return.

  28. Wonderful blog! Do you have any tips and hints for aspiring writers?

    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go
    for a paid option? There are so many options out there that I’m totally overwhelmed ..
    Any tips? Bless you!

  29. Thanks for a marvelous posting! I definitely enjoyed reading
    it, you could be a great author. I will be sure to bookmark your blog and will come back later in life.
    I want to encourage continue your great work, have a nice holiday
    weekend!

  30. Attractive section of content. I just stumbled upon your web site and in accession capital
    to assert that I acquire in fact enjoyed account your
    blog posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently fast.

  31. Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to my blog that automatically tweet my
    newest twitter updates. I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with
    something like this. Please let me know if you run into
    anything. I truly enjoy reading your blog and I look forward to your
    new updates.

  32. Woah! I’m really digging the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s very hard to get that
    “perfect balance” between user friendliness and appearance.
    I must say you have done a very good job with this. In addition, the
    blog loads super fast for me on Firefox. Excellent Blog!

  33. After going over a number of the blog articles on your site, I truly like your
    way of writing a blog. I saved it to my bookmark webpage list
    and will be checking back in the near future. Take a look at my website as well and tell me your
    opinion.

  34. Link exchange is nothing else except it is just placing the other person’s
    website link on your page at proper place and other person will also do similar in support of you.

  35. Thanks for a marvelous posting! I certainly enjoyed reading it, you can be
    a great author.I will always bookmark your blog and may
    come back sometime soon. I want to encourage continue your great writing, have a nice afternoon!

  36. Hi there! This is my 1st comment here so I just wanted to give a
    quick shout out and tell you I really enjoy reading through your posts.

    Can you recommend any other blogs/websites/forums that cover the same topics?
    Thanks a lot!

  37. My relatives all the time say that I am killing my time here at
    web, however I know I am getting experience daily by reading such fastidious articles.

  38. Thanks , I’ve just been searching for information approximately this topic for a
    long time and yours is the best I have came upon till
    now. However, what about the bottom line?
    Are you positive about the supply?

  39. Hello, I think your blog might be having browser
    compatibility issues. When I look at your blog in Safari, 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, terrific blog!

  40. My partner and I absolutely love your blog and find the majority of
    your post’s to be exactly what I’m looking for.
    Would you offer guest writers to write content for you?
    I wouldn’t mind composing a post or elaborating on a lot of the subjects
    you write related to here. Again, awesome website!

  41. Attractive component of content. I just stumbled upon your
    web site and in accession capital to assert that
    I get actually loved account your weblog posts.

    Any way I will be subscribing to your feeds and even I achievement you get admission to constantly rapidly.

  42. Oh my goodness! Impressive article dude! Thank you, However I am encountering issues with your RSS. I don’t know why I am unable to join it. Is there anybody having identical RSS problems? Anyone that knows the solution can you kindly respond? Thanks!!

  43. I’m not sure where you are getting your info, but good topic.
    I needs to spend some time learning much more or understanding more.

    Thanks for great info I was looking for this info for my mission.

  44. Hey! This post couldn’t be written any better!

    Reading this post reminds me of my good old room mate! He always kept talking about this.

    I will forward this page to him. Fairly certain he will have a good read.
    Thanks for sharing!

  45. Hey There. I found your blog using msn. This is a very well written article.
    I’ll make sure to bookmark it and return to read more of your
    useful info. Thanks for the post. I’ll certainly return.

  46. Interesting blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Many thanks

  47. Your quite own commitment to receiving the message throughout arrived to be rather effective and have consistently empowered employees just like us to attain their desired aims.

  48. Present without the answers to be able to the difficulties you’ve fixed out through this guideline is a critical situation, as well as typically the kind which could possess badly affected my whole career if I experienced not discovered your site.

Leave a Reply

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