processing collections of trees and cycles in polynomial time